1

I have a scanner asking for some preferences. It creates an int variable choice between 0 and 3. Then I do the following:

String location;
switch(choice){
    case 0:
        System.out.println("Please type the zip codes you would like to search in a comma separated list");
        location = input.nextLine();
        break;
    case 1:
        System.out.println("Please type the street names you would like to search in a comma separated list");
        location = input.nextLine().toUpperCase();
        break;
    case 2:
        System.out.println("Please type the boroughs you would like to search in a comma separated list");
        location = input.nextLine().toUpperCase();
        break;
    default:
        location = "none";
        break;
    }
String locations[] = location.split("\\s*,\\s*");

now to me this seems perfectly fine, but when choice is set to 0,1, or 2 it will print the correct line, but skip the part where the user has to input something (the line that looks like location=...)

this means that it does not give the user the chance to enter anything and therefore locations becomes a blank list. Why does this happen and how can I fix it?

Ryan Saxe
  • 14,833
  • 19
  • 66
  • 116
  • 1
    Check out this question: http://stackoverflow.com/questions/5032356/using-scanner-nextline – ebbs Oct 20 '13 at 21:11

3 Answers3

5

You are probably reading the newline after the last input, instead of the really next line. Try:

int choice = input.nextInt();

input.nextLine(); // <--- Eat it here!
String location;
switch(choice){
    case 0:
        Syst...

The nextInt() call, where you prompt the choice, does not end the line. So the first call to nextLine() after nextInt() is going to return an empty String. To fix that, eat the empty line after the integer and then move on.

Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
  • What about using new Scanner for this input? – catch23 Oct 20 '13 at 21:12
  • @nazar_art: Totally offtopic and most likely wrong, or I didn't understand what you are trying to say. – Martijn Courteaux Oct 20 '13 at 21:13
  • 1
    this was my issue...thanks! I will except your answer in 6 minutes when I am permitted to – Ryan Saxe Oct 20 '13 at 21:16
  • @Martijn Courteaux I wondering to know whether possibly to use new scanner for input, after `int choice = input.nextInt();` is executed? – catch23 Oct 20 '13 at 21:17
  • Well, that is wrong because of so many reasons. 1) You are creating unnecessary objects. 2) This might fail, if the underlying inputstream is wrapped within a BufferedInput stream that comes from a File. 3) The OP his problem is well defined behavior of a Scanner and should simply be handled as desired. 4) It is just ugly to create a new Scanner. – Martijn Courteaux Oct 20 '13 at 21:20
0

I faced the same problem as you. Try:

case 'f' :
                System.out.println("Enter a word or phrase to be found:");
                scnr.nextLine();
                String phrase = scnr.nextLine(); //
Alan
  • 1
0

Put nextLine after the read before the switch.

System.out.print("Digite um número ente 1 e 9: ");
int opc = read.nextInt();
read.nextLine();
switch(opc) {

And read your value with nextLine()

case 6:
    String[] procurados = read.nextLine().split(" ");