![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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();
}
}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! ![]() |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
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
![]() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
try this:
if ("y".equals(answerLetter))
System.out.println("Nice weather we are having.");
if ("n".equals(answerLetter))
System.out.println("Good-bye."); |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
*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. |
|
|
|
|
|
#5 | |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Quote:
__________________
"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 |
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Quote:
![]() 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 7:38 PM. |
|
|
|
|
|
|
#7 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
How about using a DataInputStream? It has a readChar method. To create one is just like with Scanner:
DataInputStream dataObject = new DataInputStream(System.in); |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() |
Quote:
"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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|