java creating a menu

Joined
Nov 26, 2006
Messages
620
ok for a project I need to create a menu that displays a list of options and waits for input from the keyboard if the input matches an menu selection then the corresponding method is called. once that method is done it must return to the menu, the menu must keep coming up unless the user presses "q". I dont think the else is right either, it seems to me that it would almost always trigger the else because else would also mean no input at all. Theres got to be a better way to do this

Heres what ive got so far, im not sure what to put in the parameter of the do- while() loop.
 
you could do something like

Code:
String input;
Scanner option = new Scanner(System.in);

while (((input = option.nextLine()) != null) && (input != "q"))
{
     if (input == "someting")
        do someting
     else if (input == somethingelse)
        do something else;
     else
        your do options
}
 
you could do something like

Code:
String input;
Scanner option = new Scanner(System.in);

while (((input = option.nextLine()) != null) && (input != "q"))
{
     if (input == "someting")
        do someting
     else if (input == somethingelse)
        do something else;
     else
        your do options
}

I just tried that but im getting an error "comparing strings using == or !="
 
you need to use string1.equals(string2) instead of ==, !string1.equals(string2)
string1==string2 compares the reference to the strings instead of the values. you can only use == reliably on primitive values (int, boolean, double, etc) and not on objects.

If string1 can be null, you need to check for that, otherwise you will get a NullPointerException
 
you need to use string1.equals(string2) instead of ==, !string1.equals(string2)
string1==string2 compares the reference to the strings instead of the values. you can only use == reliably on primitive values (int, boolean, double, etc) and not on objects.

If string1 can be null, you need to check for that, otherwise you will get a NullPointerException

what can I use that would be equivalent to !=
 
Code:
if (option.equals("a")) {
	//something
}
else if (option.equals("b")) {
	//something
}
else if (option.equals("c")) {
	//something
}
else {
	// invalid selection
}


(option.equals("a")) returns True if correct, otherwise False.
(!option.equals("a")) returns False if correct, otherwise True.

Code:
boolean checkout = false;

if (checkout) {
	System.out.println("True");
}
else if (!checkout) {
	System.out.println("False");
}
 
Ok I posted updated code (look at the original post), now it runs and works (kind of). If you type a,b,or c and press enter it displays the correct message, then it prompts you to press enter to continue (pauseit method), when you press enter it writes 300 blank lines (to simulate a new page, since you cant clear the console) and then re-displays the initial selection message. However at this point the program ends. But it shouldnt, it needs to wait for the next selection.
 
you have this
if char equals a
dostuff
if char equals b
dostuff
else
clear

if they press a, it does stuff, then checks if its equal to b, its not, so you get the clear action
you probably want all the subsequent if's to be else if's instead.
 
Just FYI guys, best practices when comparing against String literals is to invoke equals from the literal, this way you avoid a null check.

Code:
String someString;
/*
code affecting someString
*/
if("a".equals(someString)){
       /* Do something appropriate */
}else{
      /* Do a barrel roll! */
}
 
Wouldn't be worth the trouble in java, you'd have to store a,b,c,q as ints. :(
I think Java treats chars as ints (or rather, unsigned shorts) in most (all?) cases.

A switch on a char is valid, as is something like this:
Code:
int[] x = new int['z'];
x['$'] = '@';
x['x'] = 1+'x';

EDIT: But I agree - by the time you've tested for null and length!=1 and extracted the character to switch on, you've probably made things worse than the original if-elses.
 
Back
Top