I'm taking my second Java class right now and I'm trying to use an if/
else argument based on a user input. The values in question all could
be integers but if I set them up as int, the program crashes when
anything other than an integer is entered. So I tried using char.
But all of my if statements don't work anymore...
if (userInput == 1){
string
}
if (userInput == 2){
string
}
if ((userInput != 1)&&(userInput !=)){
string
}
and so on and so forth...
All of this is running in a while statement and as far as I can tell,
the if arguments are not reading the char value. I verified this by
placing the code System.out.println(userInput) right before the if
args started and verified that the value of variable userInput is
changing with the inputed value.
What am I missing here?
jt - 06 Apr 2007 15:25 GMT
> I'm taking my second Java class right now and I'm trying to use an if/
> else argument based on a user input. The values in question all could
[quoted text clipped - 23 lines]
>
> What am I missing here?
I *think* I know what the problem is, but rather than post my comments
on the solution, I would suggest two things:
a) post what is known as a SSCCE http://mindprod.com/jgloss/sscce.html
so that we can see your code in context thus allowing a complete picture.
b) check out what the API says about the String class. Pay particular
attention to the way the API explains how to do comparisons.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Signature
There are 10 types of people in this world. Those who understand binary
and those who don't.
Jason Cavett - 06 Apr 2007 15:30 GMT
> I'm taking my second Java class right now and I'm trying to use an if/
> else argument based on a user input. The values in question all could
[quoted text clipped - 26 lines]
>
> What am I missing here?
Honestly, a better way to handle it is to check the user's input
before you start doing your logic. Something like this.
// get user input as a String - call it variable "userInput"
try {
int inputVar = Integer.intValue(userInput);
if(intputVar == 1) {
// do stuff
}
// other if statements
} catch(NumberFormatException e) {
System.out.println("Please enter an integer value!");
}
I realize using an exception as a form of control isn't always the
best idea. But, I wanted to be able to easily demonstrate one way to
handle user input.
matt.briancon@gmail.com - 07 Apr 2007 08:00 GMT
> What am I missing here?
I'm not entirely sure I understand your problem but I will try to help
none the less.
>From what I can tell, the variable userInput is of the primitive type
char. You are then trying to see if userInput is a 1, a 2, etc. I
think the simple solution would be to do this:
if(userInput == '1') {
string //not sure what this means...
}
...and so on through your code.
Placing the single quotation marks around the 1 tells Java that it is
no longer an int, but instead a char. This idea should have been
covered in the first few chapters of the book you used in your
previous class.
If I did just answer your quesiton, a possible follow-up question of
you might have is "Why didn't the compiler throw any errors when you
can't compare to primitive data types?" The answer is that char's
have a sort of "corresponding" int value.
Try compiling the line
System.out.println('a' == 97);
It should print true.
Hopefully I didn't completely miss your question.