View Single Post
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