-1

I am attempting to color all words sent from my chat packet blue by default but if any word is uppercase (just the first letter of word) in the sentence (other than the first word) I would like to color it lime green. I am a beginner with java but this is what I have come up with so far. The problem is that it is still setting the color of any word blue no matter if it is uppercase or not.

public void sendChatPacket(String s, int id) {
    boolean isUpperCase = Character.isUpperCase(s.length());
    s = isUpperCase ? "<col=65280>\" + s + \"</col>" : "<col=255>\" + s + \"</col>";
}

public void showInformation(Player player) {
    sendChatPacket("I can start this quest by speaking to Hetty", 8147);
    sendChatPacket("who is in Rimmington.", 8148);
}

Any help is appreciated, thank you!

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • Never heard of an `ActionSender` class before. Nor do I understand what those arbitrary-looking numbers mean in what looks like HTML-esque syntax. Maybe they're missing quotes around them? – Makoto Sep 13 '18 at 04:55
  • 1
    I think you should read the Javadocs https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isUpperCase(int) - the parameter (int) to this method should be a unicode char – Scary Wombat Sep 13 '18 at 04:56
  • Maybe use a regex to determine presence of any uppercase chars... – ne1410s Sep 13 '18 at 04:58
  • Furthermore your text says `any word` is uppercase, so why are you testing a `char` - See also https://stackoverflow.com/questions/677561/is-there-an-existing-library-method-that-checks-if-a-string-is-all-upper-case-or – Scary Wombat Sep 13 '18 at 04:59
  • I am a beginner with java my apologies. The action sender is the class this takes place in but I have edited to void to avoid any confusion. The numbers are html color codes but everything else is java. – Andrew Eggers Sep 13 '18 at 05:00

3 Answers3

1

Replace

boolean isUpperCase = Character.isUpperCase(s.length());

with

boolean isUpperCase = s.toUpperCase().equals(s);

The problem is that you are checking if the length of the string (which is a number) is uppercase.

Also, you are re-assigning s in the last line of the method. This won't change the original string. See https://stackoverflow.com/a/40523/1039555 for more details.

Edit based on new information in comments:-

String[] words = s.split("\\s+");
for (String word : words) {
    if (Character.isUpperCase(word.charAt(0))) {
        s = s.replace(word, "<col=65280>" + word + "</col>");
    } else {
        s = s.replace(word, "<col=255>" + word + "</col>");
    }
}
Kartik
  • 7,677
  • 4
  • 28
  • 50
  • Hi there, thank you for the fast response. I have tried this and it still seems when loading up the application all words are still blue. I am trying to get the words Hetty and Rimmington to turn lime green. https://imgur.com/a/1EjAk1C (that is just one example but I want it to be automated for any future uppercase words not just those 2 words) – Andrew Eggers Sep 13 '18 at 05:04
  • *I am trying to get the words Hetty and Rimmington to turn lime green* is that case you want to check `Character.isUpperCase(s.charAt(0));` The word is not upperCase, only the first char is – Scary Wombat Sep 13 '18 at 05:07
  • (edited) fixed the error I had but not it seems to color everything lime green. – Andrew Eggers Sep 13 '18 at 05:09
  • try `boolean isUpperCase = s != null && !s.isEmpty() && Character.isUpperCase(s.charAt(0));` – Kartik Sep 13 '18 at 05:13
  • Thank you Kartik there is no longer an error but now everything is lime green. For this sentence I only wanted the word Hetty in lime green. Picture of what it is like right now: https://imgur.com/a/1UCkFVX I am trying to have everything Blue and then the word Hetty lime green since it is capitalized – Andrew Eggers Sep 13 '18 at 05:15
  • Hey there thank you for the help, I tried the code in the edit. Now the result ends up with no color being applied to the text: https://imgur.com/a/5kz2md9 – Andrew Eggers Sep 13 '18 at 05:28
  • I think you have not posted the full code in that method. Can you please update your question with proper code? – Kartik Sep 13 '18 at 05:30
  • @Kartik, string is a final object, replace doesn't change the original string just return the original string with the modifications. In java is impossible to change edit the chars into a string – Rafael Lima Sep 13 '18 at 05:35
  • I have updated the original post with all of the relevant code used. @Kartik – Andrew Eggers Sep 13 '18 at 05:36
  • Progress https://imgur.com/a/kujtdDD thank you so much @Kartik it still seems to have not colored Rimmington properly and I was trying to only color words (Hetty and Rimmington in this incidence) and not the I at the very beginning of the sentence. Sorry for being a hassle but I have learned a lot from this so far – Andrew Eggers Sep 13 '18 at 05:48
  • This will cause miserable result in string like "old golden ring", that the first "old" will cause the "old" in _g*old*en_ to be replaced – Adrian Shum Sep 13 '18 at 09:40
  • nice catch @AdrianShum! this question is wierd, `s` is getting re-assigned but this won't change the original string, still the code works according to OP.. perhaps he is using some annotations.. so I gave up on fixing this any further.. great flag though :) – Kartik Sep 13 '18 at 23:31
0

You are begginer but please next time you ask something take longer explaining your goal...

the following code will pick first color to the words which start with capital letter, the second color to all other words

if (s != null && s.length() > 0) {
            StringBuilder result = new StringBuilder(s.length() * 2);
            result.append("<col=255>");
            String[] split = s.split(" ");
            for (String t : split) {
                boolean isUpperCase = Character.isUpperCase(t.charAt(0));
                if (isUpperCase)
                    result.append("<col=65280> " + t + "</col>");
                else
                    result.append(" "+t);
            }
            result.append("</col>");
            s = result.toString();
        }

if your are printing pure html this is going to work...

anyway i strongly advise to start basics before jumping into complex things...

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
0

Not sure what you are trying to do here but whatever you assign to s is not going to take effect to caller:

public void sendChatPacket(String s, int id) {
    boolean isUpperCase = Character.isUpperCase(s.length());
    s = isUpperCase ? "<col=65280>\" + s + \"</col>" : "<col=255>\" + s + \"</col>";
}

Method parameter are passed by value in Java. Even you are assigning s to something else, the argument passed by caller is not going to be affected.

In your caller, you are not doing anything with the message you passed in either. So your code simply do nothing meaningful.

If it is not your real code, please offer code that demonstrate the problem. Tell us what the expected result is and how your example code not fulfilling your expectation.


A little recommendation next time you are asking: You are giving too much irrelevant context here. For example in this question, you should have done diagnosis yourself and should found that after calling sendChatPacket, the result is unexpected. You do not need to tell us whatever coloring. You are just manipulating a String, and the String manipulation does not work as expected. By simplifying the scenario, you could have provide a MVCE which makes people a lot easier to understand your problem (hence, offering help)

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • I am sorry I was just trying to give context that I was trying to color some words blue and the uppercase words lime green. – Andrew Eggers Sep 13 '18 at 05:58
  • There is nothing as "color" in a Java String. You are just manipulating a string by inserting some extra "tags" (which are not even HTML). You don't need to tell us what is shown on your screen (which could be caused by wrong input, or your screen logic is simply wrong), you just need to tell us what you are expecting your string manipulation function, PERIOD. – Adrian Shum Sep 13 '18 at 06:01
  • You are right. I just want capitalized words to have one specific function while none capitalized words to have another function. The color is irrelevant. – Andrew Eggers Sep 13 '18 at 06:05
  • Then please provide code that demonstrate your problem. Your code here simply did nothing meaningful as I have explained. TBH there are simply tooooo many problems in your code and you are really wasting everybodies' time to guess what you are actually looking for – Adrian Shum Sep 13 '18 at 06:19
  • And may I know the reason for downvote? My (and everybody else's) answer are all valid answers based on what OP asked. – Adrian Shum Sep 13 '18 at 06:21
  • oh sorry to make you misunderstood. That message was meant to be for the downvoter, not particular to you :) sorry for confusing you – Adrian Shum Sep 13 '18 at 06:43