7

I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some problem. This is the code:

private void inputEntries() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Continue?[Y/N]");
    while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
        System.out.println("Enter first name");
        String name = sc.nextLine();
        System.out.println("Enter surname");
        String surname = sc.nextLine();
        System.out.println("Enter number");
        int number = sc.nextInt();
        Student student = new Student(name, surname, number);
        students.add(student);
        try {
            addToFile(student);
        } catch (Exception ex) {
            Logger.getLogger(TextReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Continue?[Y/N]");
    }
}

The problem with the code above, which also happens on different methods I tried, is that when the user types Y, the Scanner will skip the first input for first name,and jump to the surname. If the user types N the loop stops correctly. Someone can explain the reason this happens, and how to overcome using Scanner class?

p.s: Doing something like while(sc.nextLine().equals("Y")), will cause the loop to terminate before getting input from user after first run of the loop.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Giannis
  • 5,286
  • 15
  • 58
  • 113

4 Answers4

9

This is because you are using Scanner#next method. And if you look at the documentation of that method, it returns the next token read.

So, when you read user input using next method, it does not read the newline at the end. Which is then read by the nextLine() inside the while loop. And thus, your firstName contains a newline without you knowing.

So, you should use nextLine() in your while rather than next().

Similar is the case with nextInt method. It also does not read the newline. So, you can read using readLine and convert it to int using Integer.parseInt. It can throw NumberFormatException if input value cannot be converted to int. So you need to handle it accordingly.

You can try the below code: -

Scanner sc = new Scanner(System.in);
System.out.println("Continue?[Y/N]");
while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
    System.out.println("Enter first name");
    String name = sc.nextLine();
    System.out.println("Enter surname");
    String surname = sc.nextLine();
    System.out.println("Enter number");
    int number = 0;
    try {
        number = Integer.parseInt(sc.nextLine());
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    System.out.println("Continue?[Y/N]");
}

But, note one thing, if you enter a value that cannot be passed to Integer.parseInt you will get an exception, and that input will be skipped. For that case, you need to handle it by using while loop.

Or, if you don't want to do that exception handling: -

You can add an empty sc.nextLine() after sc.nextInt(), that will consume the newline left over, like this: -

    // Left over part of your while loop

    String surname = sc.nextLine();
    System.out.println("Enter number");
    int number = sc.nextInt();
    sc.nextLine(); // To consume the left over newline;
    System.out.println("Continue?[Y/N]");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Indeed its working. Although Im not completely sure why the nextInt was not working.. I typed a number and hit enter.. – Giannis Oct 21 '12 at 17:16
  • @Giannis. If you see the documentation of [`nextInt`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29), it says that, it reads the `next` integer token, thus leaves the newline that will also be the part of input. – Rohit Jain Oct 21 '12 at 17:18
  • @Giannis. So, that `newline` will be read next time when you invoke `nextLine` and hence that part of reading will be skipped. – Rohit Jain Oct 21 '12 at 17:18
  • @DavidKroukamp. Thanks. :) that part is always a problem with the beginners, when they get user input with Scanner. – Rohit Jain Oct 21 '12 at 17:19
  • @RohitJain even I was missing it until I used `nextLine()` and read your post :P – David Kroukamp Oct 21 '12 at 17:20
  • Right got it now should have checked doc.. Im gonna hide in a cave and burn my degree for heat... :P – Giannis Oct 21 '12 at 17:20
  • @Giannis. Great. Remember, everytime you see strange behaviour with some pre-defined method, you always have `documentation` to look at. They say what actually that method will do. – Rohit Jain Oct 21 '12 at 17:22
  • sc.next() didnt fix the problem (after nextInt). Used sc. skip("\n") instead. – Giannis Oct 21 '12 at 17:27
  • @Giannis. Oh! Sorry. sc.next() will not read the `newline`. It just reads next token. You can use `sc.nextLine` instead. I'll improve my post. – Rohit Jain Oct 21 '12 at 17:28
4
  • Use equalsIgnoreCase(..) to prevent the case of Y or N being lower case and vice versa.
  • Don't use sc.next() rather sc.nextLine() to achieve what you want, because next() only reads up to the next space while nextLine() reads up to the next \n break.
  • Don't use nextInt() read all data as String and then convert because nextInt() is like next() thus is does not read up to the next \n.

Try it like this:

Scanner sc = new Scanner(System.in);
System.out.println("Continue?[Y/N]");

while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here

    System.out.println("Enter first name");
    String name = sc.nextLine();

    System.out.println("Enter surname");
    String surname = sc.nextLine();

    System.out.println("Enter number");

    int number=0;

    try {
    number = Integer.parseInt(sc.nextLine());//read int as string using nextLine() and parse
    }catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }

    System.out.println("Continue?[Y/N]");
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Continue?[Y/N] Y Enter first name dsa Enter surname asd Enter number 21 Continue?[Y/N] Y BUILD SUCCESSFUL (total time: 15 seconds) Thats what happens. – Giannis Oct 21 '12 at 16:57
  • I copy pasted the while line and the thing shown on comment happens. Is it working for you?? – Giannis Oct 21 '12 at 17:01
  • @Giannis you have `Enter number 21 Continue?` you did not include that in yourquestions snippet include entire code something else may be at fault – David Kroukamp Oct 21 '12 at 17:02
  • @Rohit Jain figured I should use nextLine and parseInt.. Its still strange that nextInt cause problem tho.. – Giannis Oct 21 '12 at 17:17
  • @Giannis as said its because `nextInt` reads next token and not up to the next newline – David Kroukamp Oct 21 '12 at 17:19
  • @Giannis. I have given a work around in my post, for not using exception handling. You can look at that. – Rohit Jain Oct 21 '12 at 17:19
1

You could use

String abc = "abc";
String answer = "null";
while (abc.equals("abc")
{
if (answer.equals("n")
{
break;
}
while (answer.equals("y"))
{
//Put your other code here
System.out.print("Continue? [y/n]")
answer = input.nextLine();
if (answer.equals("n")
{
break;
}
}
}

That should stop the program when the input is

"n"

You will also have to import and create the Scanner like this:

//This is how you import the Scanner
import java.util.Scanner;
//This is how you create the Scanner
Scanner input = new Scanner(System.in);
/**
*The name
*/
"input"
/**
*is used because of
*/
input.nextLine
marisusis
  • 342
  • 4
  • 20
1
public void display() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = i; j < 5; j++) {
            System.out.print(" ");
        }
        for (j = 1; j < (2 * i); j++) {
            System.out.print(i % 2);
        }
        for (j = 1; j < ((5 * 2) - (i * 2)); j++) {
            System.out.print(" ");
        }
        for (j = 1; j < (2 * i); j++) {
            System.out.print(j);
        }
        System.out.println();
    }
    for (i = 4; i >= 1; i--) {
        for (j = i; j < 5; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= i; j++) {
            System.out.print(j);
        }
        for (j = i - 1; j >= 1; j--) {
            System.out.print(j);
        }
        for (j = 1; j < ((5 * 2) - (i * 2)); j++) {
            System.out.print(" ");
        }
        for (j = 1; j < (2 * i); j++) {
            System.out.print(j);
        }
        System.out.println();
    }
}
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51