0

I have this piece of code that is supposed to add String values to an arrayList through the Scanner's next() or nextLine() method. The problem with next() is that it ignores everything after the first white space so I get that I should use the nextLine() method instead. The problem with nextLine() is that that it doesn't record the input, instead it stores a couple of blank spaces into the arrayList. Here's the code:

System.out.println("\nWhat is your idea? ");                  
String i = in.nextLine();                               
in.nextLine();
meals.add(i);    
System.out.println("\n" + i + " has been entered into the idea pool. \n");
System.in.read();

I added the extra "in.nextLine()" after the initial "String i = in.nextLine()" because that's the only fix I found when I researched this problem but it doesn't work for me, it just still just stores a couple of blank spaces. Also, the System.in.read(); at the end there is only there so that it doesn't just jump forward after taking the input.

Here is the code where the above sample fits into:

ArrayList<String> meals = new ArrayList<String>();
String select = "";
while(!select.equals("")){
    System.out.println("What would you like to do?");
    System.out.println("1. <Irrelevant>");
    System.out.println("2. Enter an idea");
    System.out.println("3. <Irrelevant>");
    System.out.println("4. <Irrelevant>");
    System.out.println("Q. <Irrelevant>");

    select = in.next();

    switch(select){
        case "1":
            //Some stuff here.
        case "2":
            //Here's where the above problem fits into.
        case "3":
            //More stuff here
        //and so on...    
    }
}
L. Grobler
  • 25
  • 7

1 Answers1

1

The reason why you are facing such issue is because you are using firstly next() method to read the input, and the further inputs you are taking using nextLine().

next() accepts the input, and the input pointer stays on the same line as of the current input.

So, as soon as you enter your choice and hit Enter, the choice is saved into the select variable, but, the input pointer is still on the same line. You should use a nextLine() to move the pointer to a new line.

Then you should use any number of nextLine()'s to receive multiple lines.

Also, remove an extra nextLine() method call from the case 2 statement. Remove the System.in.read() too, as your problem would have been solved.

ArrayList<String> meals = new ArrayList<String>();
String select = "";
while(!select.equals("")){
System.out.println("What would you like to do?");
System.out.println("1. <Irrelevant>");
System.out.println("2. Enter an idea");
System.out.println("3. <Irrelevant>");
System.out.println("4. <Irrelevant>");
System.out.println("Q. <Irrelevant>");

select = in.next();
in.nextLine();   // add this extra line in your code
switch(select){
    case "1":
        //Some stuff here.
    case "2":
        System.out.println("\nWhat is your idea? ");                  
        String i = in.nextLine();                               
        meals.add(i);    
        System.out.println("\n" + i + " has been entered into the idea pool. \n");
    case "3":
        //More stuff here
    //and so on...    
}
Am_I_Helpful
  • 17,636
  • 7
  • 44
  • 68
  • That really did the trick. I never knew that if you're using next() or nextLine() that you would have to keep using it. Thank you for your help. – L. Grobler Jan 08 '16 at 16:32
  • The appropriate action would've been to vote to close as duplicate of the many. Here's one: http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods?rq=1 – Sotirios Delimanolis Jan 08 '16 at 16:33
  • Why not just use `select = in.nextLine()` here, and completely get rid of `in.next()`? – Tricky12 Jan 08 '16 at 16:34
  • @SotiriosDelimanolis - Thanks, but, I really didn't think that I should vote to close that. I'll take care from the next time for sure. Also, see my below comment to Tricky12. – Am_I_Helpful Jan 08 '16 at 16:35
  • @Tricky12 - Well, it seems OP's wish is to use `next()` to achieve the desired output. SO, I gave OP the clear concept alongwith the answer. Both would benefit him. – Am_I_Helpful Jan 08 '16 at 16:36