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.
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
Joined: Mar 28, 2003
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 ]
Joined: Jul 27, 2001
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
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'.
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().
Joined: Mar 28, 2003
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.
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 ]