variable type char used in boolean arg

L

Les

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?
 
J

jt

Les said:
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?
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
 
J

Jason Cavett

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?

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.
 
M

matt.briancon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top