hi,
i receive an error on this line -- if (salesPersonsName <! "end") --
error : operator ! cannot be applied to java.lang.String
what can i do?
thanks
hi,
i receive an error on this line -- if (salesPersonsName <! "end") --
error : operator ! cannot be applied to java.lang.String
what can i do?
thanks
Short answer: the unary ! operator only works on boolean values, not on String objects — is correct. The original <! looks like a mistyped attempt to express “not equal” or “not 'end'”. For string content checks you need a boolean expression that compares the string contents, and then you can negate that boolean.
Practical, null-safe patterns to avoid the common NullPointerException: prefer comparing a constant to the variable or use java.util.Objects. For example, build a trimmed, non-null value first and then test it; or use Objects.equals for a null-tolerant equality test. These approaches avoid the common bug where the input is null or has stray whitespace.
If you want case-insensitive sentinel handling use a case-insensitive comparison; if you actually need ordering (alphabetical greater/less), use a lexicographic comparison and check the returned integer as suggested. Avoid using == to test string content — that checks reference identity, not content, and will often give the wrong result.
Quick checklist:
Objects.equals(a, b) or "end".equals(variable)-style constant-first checks to prevent NPEs.References in thread: (OP), (note about booleans), (pointing toward equals), and (ordering via compare).
Jump to Post— jwenting 1,905you can't negate a String, only a boolean.
I've no clue what you're trying to accomplish there so can't help you out.
Jump to Post— masijade 1,351Are you trying to say "not less than", if so why don't you just say "greater than"?
> // instead of <!Edit: Nevermind, I'm an idiot, they're Strings so greater than and less than are not what you want anyway. Doh!
you can't negate a String, only a boolean.
I've no clue what you're trying to accomplish there so can't help you out.
Are you trying to say "not less than", if so why don't you just say "greater than"?
>
// instead of
<! Edit: Nevermind, I'm an idiot, they're Strings so greater than and less than are not what you want anyway. Doh!
if you want to compare strings use compareTo() method ex: oneString.compareTo(anotherString). if oneString is greater then anotherString the method will return value >0, it it is less --> value <0, and if they are equal -->value =0.
I hope that this will help you.
Cao
I knid of doubt that will help though, as the majority of peoples names (as the variable implies that that that is) will not be alphabetically less than end. What the OP probably wants is if (!salesPersonsName.equals("end")) . And this thread is bit old, anyway. I doubt the OP is still looking for a solution.
I knid of doubt that will help though, as the majority of peoples names (as the variable implies that that that is) will not be alphabetically less than end. What the OP probably wants is
if (!salesPersonsName.equals("end")). And this thread is bit old, anyway. I doubt the OP is still looking for a solution.
I guess I'm missing something here. Unusual for you to reply an old thread. Did somebody posted something? Did any moderator moved anything?
Someone must have moved something, or something, as this popped up in my mail as if it had been replied to, and now that I look at the post above mine, I realise that that post is old too, I had just assumed it was just posted. ;-) Ah well, sheet happens.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.