File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes The operator | is undefined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The operator | is undefined " Watch "The operator | is undefined " New topic
Author

The operator | is undefined

Jairo Navarrete
Greenhorn

Joined: Mar 07, 2013
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11473
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jairo Navarrete
Greenhorn

Joined: Mar 07, 2013
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11473
    
  16

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

Joined: Mar 07, 2013
Posts: 11
cool thanks I rewrote it though to this now it works fine for every result


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

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...


luck, db
There are no new questions, but there may be new answers.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 07, 2013
Posts: 11
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 07, 2013
Posts: 11
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Mar 17, 2011
Posts: 8176
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi ,

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

Regards,
James
Jairo Navarrete
Greenhorn

Joined: Mar 07, 2013
Posts: 11
How would I be able to use if (true) with the equalsIgnoreCase()? Is there another way to ignore the Casing ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 07, 2013
Posts: 11
Oh ok haha I was confused. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The operator | is undefined