• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equalsIgnoreCase

 
Debbie Chaney
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make this so that whatever the user enters, a "Y" or a "y" or a "N" or a "n", it will have no effect on the outcome. In my book, equalsIgnoreCase is supposed to be used with a boolean, but I have "again" as a character. I'm not sure how to write it.

System.out.println ("Do you want to enter a new job? "
+ "(\"Y\" to do this again, \"N\" to see all results.)");
again = write.nextChar();

}

while (again != N);
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equalsIgnoreCase() method is used to compare String objects. You can either convert the char to a String (in which case, you'll be able to use that method), or you can simply compare char values with the == operator.

Example:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Debbie Chaney:
...equalsIgnoreCase is supposed to be used with a boolean...

equalsIgnoreCase is a method in the String class that takes an Object (presumably a String) as an argument and returns a boolean. But in your situation, you appear to be using primitive chars rather than Strings.

If you're using chars, you could simply test for either case of 'Y'...

if( (myChar == 'Y') || (myChar == 'y') ) {...}

Note that char literals are enclosed in single quotes.
 
jin sun
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you could just aoply the toLowerCase method on input and compare the input with 'y' and 'n'.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic