Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2006, 10:49 PM   #1
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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
tumbleTetris is offline   Reply With Quote
Old May 20th, 2006, 10:57 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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
Wizard1988 is offline   Reply With Quote
Old May 20th, 2006, 11:00 PM   #3
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
sorry, how do I go about that?
tumbleTetris is offline   Reply With Quote
Old May 20th, 2006, 11:12 PM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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
}
Wizard1988 is offline   Reply With Quote
Old May 20th, 2006, 11:15 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 931
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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
}
titaniumdecoy is offline   Reply With Quote
Old May 20th, 2006, 11:27 PM   #6
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
Ok, I got it to work.

But I don't understand, if I
int input = kbd.read();

if((char)input == 'x')
{
  //do x option
}
works, however

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
tumbleTetris is offline   Reply With Quote
Old May 21st, 2006, 7:37 AM   #7
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,194
Rep Power: 5 lectricpharaoh will become famous soon enough
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)
use
if(true == flag)
This way, if you mis-type the == as =, the compiler will catch it, and give you an error (since you cannot assign to a constant). You can use this trick with literal constants, variables declared as final in Java or const in C/C++, or with enumerated types. The trick works in Java, C, C++, C#, and I'd expect it works in any other languages that use a C/C++ style syntax.
__________________
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
lectricpharaoh is online now   Reply With Quote
Old May 20th, 2006, 11:43 PM   #8
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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
}
Wizard1988 is offline   Reply With Quote
Old May 21st, 2006, 8:42 AM   #9
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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
tumbleTetris is offline   Reply With Quote
Old May 21st, 2006, 9:33 AM   #10
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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?
tumbleTetris is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC