aspose file tools*
The moose likes Beginning Java and the fly likes equalsIgnoreCase Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equalsIgnoreCase" Watch "equalsIgnoreCase" New topic
Author

equalsIgnoreCase

Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
The last piece of my puzzle - I don't understand how to use the equalsIgnoreCase method. In my program I ask the user to input a 'c' or 'f' for celcius or farenheit. I want my program to ignore the case and accept either upper or lowercase. I have searched the internet for the past two days and every site I found (including Java Sun Tutorial and Java Ranch) doesn't break it down clearly to me. Haven't heard a peep from my homework board since I posted early this morning so I'm back here for help.


The above code gives me an error that says:

Also any suggestions on a website that may help me get a grip cause this textbook is clear as mud.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Your declaration of fahrenheitCelsius probably looks something like this:

Note that fahrenheitCelsius is not a java.lang.String -- it's a char. SavitchIn.readLineNonwhiteChar() returns a char.
Try changing the declaration of char fahrenheitCelsius to this:

And replacing

with

[ March 28, 2003: Message edited by: David Weitzman ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jem,
Apparently your variable fahrenheitCelsius is declared as a char. Since a char is a primative, the compiler is telling you that it cannot be dereferened. The equalsIgnoreCase() is a member of the String class. So, if you want to use that method you'll need a String. You can do it something like this:

You may also want to take a look at the object wrapper for char, the Character class in the API docs.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
Thank you David and Michael. You were both exactly right about the declaration of fahrenheitCelsius. I tried both solutions and both gave me a couple of compiling errors. So after working on errors for both programs I was able to get the program with David's solution to compile however now I can't get it to give me an output.
Previously I was able to get the program to do the conversions as long as I entered an uppercase C or F but now when I enter any case it gives me the System.out.println stmt to "Enter "C" for Celsius or "F" for Fahrenheit:" which should only do it there is any character than c,C,f,or F.
I've narrowed down my errors using Michael's solution and I'll try to fix those also so I can compile and see if it runs. I figure still trying to fix the errors will help me learn more about Java.
Would someone please give me an opinion on why my program no longer gives me the answer?
Thank you!

I almost feel embarrassed posting this little code among you more experienced folks
[ March 29, 2003: Message edited by: Jem Edwards ]
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I was expecting that you might come across this . But it's good for you to know anyway. You're not using equalsIgnoreCase(). Even so, you might expect

to be 'true' when fahrenheitCelsuis is "C". Not so.
First read Cup Size -- a story about variables. Then try executing this code:

That's was a long-winded way of saying you need to replace (fahrenheitCelsius!="C") with (!fahrenheitCelsius.equalsIgnoreCase("C")) etc.
Actually you should probably follow Michael's suggestion and look at the Character class. Applying Character.toUpperCase(char ch) to the input you get from the user would make sure that fahrenheitCelsiusAsChar == 'C' returns true whether the user entered 'c' or 'C'.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Your other option is to read in an actual String from the console. You can do this by wrapping System.in with a BufferedReader and using BufferedReader.readLine().
Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
Thanks to you all. I have printed your responses to learn from and I've printed the four Campfire stories. I'll be rereading the first three chapters of this textbook and the campfire stories tonight and tomorrow. I just can't seem to get a grasp of this stuff and hopefully rereading this stuff again since I've worked on this exercise may help me absorb something.
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
You had a very good idea to use readLineNonWhiteChar(). The problem that you encountered in the first place is that you can't call a method on a primitive (char is a primitive returned from readLineNonWhiteChar).
You can do a couple of other things:
Take this:
char input = SavitchIn.readLineNonWhiteChar();

opt 1) check the char value with == operator
opt 2) convert to a string
opt 3) use methods of Character class
opt 4) do a switch

[ March 31, 2003: Message edited by: Garrett Smith ]
[ March 31, 2003: Message edited by: Garrett Smith ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
 
jQuery in Action, 2nd edition
 
subject: equalsIgnoreCase