![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
how can x be a valid choice in type int?
I'm making a menu, it has 3 choices, the last is x which is cancel.
I don't understand how I can get the computer to understand x as an x since I have int choice = keyboard.nextInt(); and I know that it won't except anything of type 'char'. The menu looks something like this: 1. Choice A 2. Choice B x. Cancel |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
First get the ASCII value of the key pressed check to see if it is the same as x and then check if it is 1 or 2
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
sorry, how do I go about that?
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
I haven't toyed around with Java for quite some time but i think that you should read the input and then cast it to an integer.
char input = kbd.read();
if((int)input == 'x')
{
//do x option
}
if(input == '1')
{
//do option 1
} |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Why not use the char returned by keyboard.nextInt()?
char choice = keyboard.nextInt();
switch (choice) {
case '1':
// do something
break;
case '2':
// do something else
break;
default:
// otherwise do this
} |
|
|
|
|
|
#6 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
Ok, I got it to work.
But I don't understand, if I int input = kbd.read();
if((char)input == 'x')
{
//do x option
}if(input = '1')
{
//do option 1
}doesn't, I made it 1 equal because it was of type int (I thinkt hats how its supposed to be). I think I'm just making an obvious mistake here, but I'm kind of new to programming so I make a lot of mistakes like this ![]() |
|
|
|
|
|
#7 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,194
Rep Power: 5
![]() |
As Wizard1988 says, you need to use == for comparisons, not =. The former is a test for equality, and the latter is an assignment. In some languages, like C and C++, any nonzero result from an expression is treated as true. In Java, this is not the case, but if you do an assignment into a boolean variable, then the compiler will not flag this as an error.
One trick when comparing to a constant is to put the constant on the left hand side of the expression. For example, instead of if(flag == true) if(true == flag)
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
Don't worry about not knowing this, it is just part of programming. You learn things as you go along.
If you put anything in single quotes as in 'x' or '1' you take its ASCII value. You can see how this works by casting chars to integers and the other way around. You should also fix your if statement it should be if(input == '1')
{
//do option 1
}instead of if(input = '1')
{
//do option 1
} |
|
|
|
|
|
#9 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
thank you everyone
![]() I was wondering if I was doing what I was doing in the most efficient manner possible. The program is supposed to calculate ticket prices, there are two different sessions and 3 different seating plans. What I'm doing is asking for the session time in and then in two different statements I am basically asking for the seats, I don't think this is a very efficient way as I basically repeat myself twice, and was wondering if anyone thought of a better way of doing it |
|
|
|
|
|
#10 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
ooh, another little technicality.
if a user inputs something that is not 1 2 or x, how can I tell them they made a mistake and should reinput something 1 2 or x. If I use else if's I can't ask again, can I? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|