Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2005, 1:50 PM   #1
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Logical Error... :/

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:

import java.util.*;

public class FirstProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello out there.");
        System.out.println("Want to talk some more?");
        System.out.println("Answer y for yes or n for no.");

        Scanner scannerObject = new Scanner(System.in);
        String answerLetter;
        answerLetter = scannerObject.next();
        
        if (answerLetter == "y")
            System.out.println("Nice weather we are having.");
        if (answerLetter == "n")
        	System.out.println("Good-bye.");

        System.out.println("Press Enter key to end program.");
        String junk;
        junk = scannerObject.next();
    }
}
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

nextInt();
nextDouble();
nextLong();
...
etc.

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:

 
if (answerLetter = "y")
    System.out.println("Nice weather we are having.");
if (answerLetter = "n")
    System.out.println("Good-bye.");

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!
shangnyun is offline   Reply With Quote
Old Jun 7th, 2005, 1:56 PM   #2
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
Wait wait wait (brain flooded).. I've grown up on Java 1.2 and 1.4. What is the scanner class supposed to accomplish in Java 5? If I knew that, I would be of more help
Knight is offline   Reply With Quote
Old Jun 7th, 2005, 3:00 PM   #3
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
try this:
        if ("y".equals(answerLetter))
            System.out.println("Nice weather we are having.");
        if ("n".equals(answerLetter))
        	System.out.println("Good-bye.");
Gilward Kukel is offline   Reply With Quote
Old Jun 7th, 2005, 3:22 PM   #4
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
*smacks himself in the head*
How could I forget that! I always seem to miss the little things :p . Gilward Kukel is correct. When comparing Strings, make sure to use the equals() method or the equalsIgnoreCase() method. If you're using the == to compare (2) strings, if I remember correctly, it will compare their memory addresses, which will not be the same thus returning false.
Knight is offline   Reply With Quote
Old Jun 7th, 2005, 3:34 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Quote:
Originally Posted by Knight
Wait wait wait (brain flooded).. I've grown up on Java 1.2 and 1.4. What is the scanner class supposed to accomplish in Java 5? If I knew that, I would be of more help
Scanner is just another way to get input; in my opinion, much more efficient than BufferedReader.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jun 7th, 2005, 8:33 PM   #6
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Quote:
Originally Posted by Gilward Kukel
try this:
        if ("y".equals(answerLetter))
            System.out.println("Nice weather we are having.");
        if ("n".equals(answerLetter))
        	System.out.println("Good-bye.");
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:

public class FirstProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello out there.");
        System.out.println("Want to talk some more?");
        System.out.println("Answer y for yes or n for no.");

        char answerLetter;
        answerLetter = SavitchIn.readLineNonwhiteChar();
        if (answerLetter == 'y')
            System.out.println("Nice weather we are having.");

        System.out.println("Good-bye.");

        System.out.println("Press Enter key to end program.");
        String junk;
        junk = SavitchIn.readLine();
    }
}

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!

PS
Here's a info-link about the SavitchIn class just in case you're wondering: http://www.fas.harvard.edu/~libe50a/...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.

Last edited by shangnyun; Jun 7th, 2005 at 8:38 PM.
shangnyun is offline   Reply With Quote
Old Jun 7th, 2005, 8:41 PM   #7
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
How about using a DataInputStream? It has a readChar method. To create one is just like with Scanner:

DataInputStream dataObject = new DataInputStream(System.in);
Dameon is offline   Reply With Quote
Old Jun 7th, 2005, 8:46 PM   #8
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
I would... but I know absolutely nothing about that . My book does not at all mention that in the first chapter, and I'd like to stick to the book. However, given that I don't have the book the problem is from, that statement sorta goes out the window right there...

This is the very FIRST problem in the book, from chapter 1! WTF... why can't I get this right? OMG, what will do with chapter 4 problems? Daaaayum
shangnyun is offline   Reply With Quote
Old Jun 7th, 2005, 8:48 PM   #9
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Now that I think about it... could someone please just tell me how to use the SavitchIn class? It'll be outdated that way, but I will be done and the professor uses the old book anyway, so he's EXPECTING SavitchIn to be used. He doesn't care how we do it, so it'd be cool if I could use the Scanner class, but we're obviously having problems here . So... how do I use a random class that's not part of the standard Java package? See that link I posted earlier on info on the class. I have a SavitchIn.java file. Wha do I do with that in order to be able to use it as a class for keyboard input in my program? Thank you.
shangnyun is offline   Reply With Quote
Old Jun 8th, 2005, 3:51 AM   #10
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
Quote:
Originally Posted by Knight
*smacks himself in the head*
How could I forget that! I always seem to miss the little things :p . Gilward Kukel is correct. When comparing Strings, make sure to use the equals() method or the equalsIgnoreCase() method. If you're using the == to compare (2) strings, if I remember correctly, it will compare their memory addresses, which will not be the same thus returning false.
oh the amount of times i have done that is unbelivable and every time i sit there fror a good half an hour going well

"a" does equal "a" so "a" == "a" is okay (change "a" to variable names or seomthing)
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC