wood burning stoves 2.0
The moose likes Beginning Java and the fly likes Logical Error :/ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logical Error :/" Watch "Logical Error :/" New topic

Logical Error :/


Joined: Apr 19, 2005
Posts: 25
Help me out here, boys. I'm a newb-programmer and am really liking Java so far. I'm stuck on this problem though. Here's the code:

This is a problem from my book. I took the provided source-code and was supposed to change it around so that the program returns something when the user inputs the char "n". Now, this source-code comes from a book that was originally based on Java 1.4.2, so the author, Walter Savitch, provided a keyboard input class called SavitchIn.java. I have his class and could use it, but I figured I might as well try getting used to the newly introduced Scanner class in Java 5.0. Well, I'm not so sure if that was a good idea anymore. The Scanner class is driving me NUTS.
The problem here is that I need to input a CHAR from the keyboard. I searched myself dumb to find if there is a char-specific input method for the Scanner class, and found nothing. There are


a bunch of them like that... but no nextChar(); exists! So I figured I could try using the method for String input instead, which is just next();
So far, so good. I used this at first:

and I got no compile errors with this, but run-time errors. The compiler told me that boolean values are required here. I have no clue why... so I substituted the "="s with =='s and voila, no compile errors, no run-time errors. Great. BUT now the program has a logical error, which again I don't quite understand. It doesn't matter whether you input "y" or "n", it will always just print "Press Enter key to end program" instead of printing any of the strings in the IF statements

The code is obvious/simple enoug... help a brother out!

I break through the boundaries of the color-spectrum of "green", into unforeseen depths of greenhornia. Bare with me :/
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

See my comment on your code the problem, basically don't use == for comparison of Strings use the equals() method of that class. Also you need to look into somekind of loop if you want to ask more than one question. Have you thought about how to close the program elegantly?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11861

when you had

you are assigning the string "y" to the String answer letter. this does NOT result in a boolean. the ONLY thing that can be inside the parens of an "if" condition is a boolean. hence the problem.

now, when you changed it to "==", you are now doing a compare. but using "==" is almost ALWAYS going to be the wrong choice. Especially for strings. if you search in this forum, you'll find dozens of threads discussing this topic.

But good for you for persevering!!!
[ June 07, 2005: Message edited by: fred rosenberger ]

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

Joined: Apr 19, 2005
Posts: 25
I realize that "==" was not the right choice, and I pointed this out in the very fist post. With Strings, "==" will compare the addresses at which the strings are stored in memory, not their values. For strings, one is to use the equals() and equalsIgnoreCase() methods in order to achieve the desired result.

Anyway... take a look at the following post I made in a different forum, but on the exact same topic. It fits in here perfectly:


Gilward Kukel wrote:try this:

That worked!! Thanks.. the stupid thing is I even knew I had to use the "equals" method... but never got around to it. Thanks a lot for making that crystal-clear to me

One more thing... here is the ORIGINAL, unchanged source-code, that I am supposed to edit in such a way that the program responds with a message when the user enteres 'n' at the prompt. Those are the precise words of the assignment:

What do you think now... did I do a good job on this or did I miss anything/do too much? Also, keep in mind that this is a programming project from the very FIRST chapter from an introductory Java programming book. Hence, this problem is supposed to be really, really simple. No advanced classes/methods, absolutely nothing fancy... I just have to edit the above code in such a way that the program responds with a message when the user enters 'n' at the prompt.
What's bugging me royally is that I can't seem to find a Scanner method that will allow specifically for CHAR input. It sucks that I have to resort to String input, because it is not as exact. CHAR is exactly what I need

Thanks for all your quick help, guys!

Here's a info-link about the SavitchIn class just in case you're wondering: http://www.fas.harvard.edu/~libe50a/utilsJavadoc/docs/utils.SavitchIn.html

The reason that class exists is because Java 1.5.0 or 5.0 (whatever it is they call it now), Java had no library dedicated to keyboard input. Now it does, in 5.0, with the Scanner class. That's why all this is a bit confusing. I have the 4th edition book, class uses the 3rd edition. 3rd = Java 1.4.2 -- my book, 4th = Java 1.5.0/5.0.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Logical Error :/
It's not a secret anymore!