Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The operator | is undefined

 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how to fix this problem. Can anyone explain what I did wrong please ? What data type I should be using ?

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator | is undefined for the argument type(s) boolean, String



Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not valid java syntax:



You have to do it this way:



Of course, that won't work either. You really don't want to compare Strings with the == or != operator. You really want to use the .equals() method:



Even BETTER would be to look at the API for the String class to see if there is something that would ignore the case of the comparison...
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thanks, I did this and it worked...



but I also did this and it gave me no errors...



either of them correct way ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are correct syntax (i.e. valid as far as the language is concerned). I do not know if they are right LOGICIALLY. You may get different results...I don't have time to figure out the logical difference between the two at the moment. That is probably something you'll have to work out.
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thanks I rewrote it though to this now it works fine for every result


 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you missed the last line of fred's first response (emphasis added:
fred rosenberger wrote:Even BETTER would be to look at the API for the String class to see if there is something that would ignore the case of the comparison...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:I think you missed the last line of fred's first response (emphasis added:
fred rosenberger wrote:Even BETTER would be to look at the API for the String class to see if there is something that would ignore the case of the comparison...


@Jairo: For example, what should happen if the response is "JaIrO"? What will happen in that case with your current code?
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Ok. Well I'm just beginning so I wasn't really sure what he meant by that.

And thank you very much Darryl and Jeff for the example

I was looking at the http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

and on the method summary and i found

compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring case differences.

or might be this i need

equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case considerations.

I'll rewrite it using the latter and see how it comes out
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jairo Navarrete wrote:
and on the method summary and i found

compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring case differences.

or might be this i need

equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case considerations.


You wouldn't normally clal compareToIgnoreCase() directly unless you were writing a case-insensitive Comparator. If you're just interested in seeing if two Strings are equal or not (without caring about case), then equalsIgnoreCase() fits the bill. You could still use compareToIgnoreCase(), but that's not really what it's meant for, so it's a slightly more indirect route, and it would be a non-standard approach, which means when somebody looks at your code later (including you), the question "Why did he do it that way?" will probably come up.
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BAM !

I hope this is what you guys meant

It works like a charm



@Jeff Thanks for clearing that up I'll look more into the compareToIgnoreCase
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . .

. . .
I can think of a much simpler way to write that line:
if (true)
There is of course a form even simpler
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I can think of a much simpler way to write that line:
if (true)

@Jairo: Just one of the many reasons to avoid negative logic if you can.

Winston
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

better use brackets when ever you give condition so that these type of small errors can be removed.

Regards,
James
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I be able to use if (true) with the equalsIgnoreCase()? Is there another way to ignore the Casing ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jairo Navarrete wrote:How would I be able to use if (true) with the equalsIgnoreCase()? Is there another way to ignore the Casing ?


You wouldn't use if (true). The point was that doing



when A, B, and C are all different is the same as doing


because (X != A || X !=B || X != C) will always be true no matter what, except of course when X, A, B, and C are all equal.

Make sure you note the difference betwee

and


or, to simplify it a bit, note that !P || !Q is different from !(P || Q). (Review DeMorgan's laws if you don't understand why.)
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok haha I was confused. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic