Take the 2-minute tour ×

I have a switch statement acting as a menu, in this I am trying to read the users input. Currently I am using variable=in.next(); and this works. However it will only read one word and at points the user may need to enter more, so I tried using variable=in.nextLine();, which compiles, but when I run the program, I select my choice from the menu, and it skips the reading in and return to the menu.

Any help would be appreciated, thanks :)

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

Just use:



String choice = in.nextLine();

This should be in the constructor, and at the top of runApp.

That way, you're not leaving the new line in the buffer (where it will be used for e.g. dp).

You should have:


as described in your question.

share|improve this answer
I still have the same problem, unless I've misunderstood your answer, it's very likely since I haven't been programming for very long. –  Ben Smith Nov 26 '10 at 22:16
@Ben, I tested it with your code. I had to comment out the calls to methods you didn't include. Can you post your current code (you can edit the question and use the 101 button for formatting)? –  Matthew Flaschen Nov 26 '10 at 22:24
can't get the editor to work properly, hope you can make sense of that. i have put dp=in.nextLine(); as you can see but that is what gives me the error. –  Ben Smith Nov 26 '10 at 22:32
@Ben, right, but you didn't change the String choice line at the top. –  Matthew Flaschen Nov 26 '10 at 22:35
done that, it compiles but now i'm getting this when I type start and hit enter to bring up the menu: java.lang.StringIndexOutOfBoundsException: String index out of range: 0 (in java.lang.String) –  Ben Smith Nov 26 '10 at 22:39
show 3 more comments

You either have to strip the newline character \n from the user input or assume it's there in your switch statement.

Pretty much because you decided to use nextLine() the user input to the computer will look like this


So just compare the strings accordingly!

share|improve this answer
add comment

OK I think I am too inept at Java and have put my question badly.

When I take the users input it takes only 1 word, I want it to take everything they put basically, and when I use nextLine, this just skips the reading and takes me back to selecting a choice.

share|improve this answer
How did you get a simple link to a dropbox file? –  javamonkey79 Nov 26 '10 at 22:09
right click the file in explorer, dropbox, copy public link. any chance of some help with the problem? :P –  Ben Smith Nov 26 '10 at 22:12
add comment

Switch selector can be only integer, short, char or enum. String cannot be used as a switch selector.

If I understood you correctly you would like to control your flow using words entered by user. If you have predefined list of words I'd suggest you to use enum:

enum Words { start, stop, beep, }

Now user enters a word beep. You can say:

Words command = Words.valueOf();
switch (command) {
    case start: /* start something */ break;
    case stop: /* stop something */ break;
    case beep: /* beep!!! */ break;
    default: throw new IllegalArgumentException("Unknown command " + command);
share|improve this answer
See my comment to vic's answer. It wouldn't compile. –  Matthew Flaschen Nov 26 '10 at 22:05
add comment

Your Answer


