This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equalsIgnoreCase

 
Debbie Chaney
Greenhorn
Posts: 1
  • 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
  • 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
  • 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
  • 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'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic