Bhavesh
Bhavesh

Reputation: 4677

A simple Java code that doesn't work well

The following simple code in Java behaves somewhat in a strange way that I can not understand.

final public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\nHow many names?   ");

        int n = sc.nextInt();
        String[] a = new String[n];

        a[0] = sc.nextLine(); //This line serves no purpose at all. It's useless and should be removed.

        for (int i=0; i<n; i++)
        {
            System.out.print("\nEnter the name:->");
            a[i] = sc.nextLine(); //request for input only inside the loop.
        }

        for (int i=0; i<a.length; i++)
        {
            System.out.println(a[i]);
        }
    }
}

The above is working well with no problem at all and displays the number of names inputted into the array a[] on the console but when I remove the line a[0] = sc.nextLine(); //This line serves no purpose at all. It's useless and should be removed., it displays for number of users first. let's say 3. there is no problem but when the loop starts iterating, it will ask for the name and first time the message Enter the name:-> is displayed twice


and the output would be something like shown below.

How many names? 3

Enter the name:-> Don't allow to enter the name here.

Enter the name:->Tiger

Enter the name:->Pitter

Tiger

Pitter


Although I entered 3 for "How many names?", it allows only two names to enter. Why?


Note again that the code shown above is working well. The problem occurs only when the line specified with bold latters in the above paragraph is commented out.

Upvotes: 0

Views: 156

Answers (3)

JB Nizet
JB Nizet

Reputation: 691755

The nextInt call reads from input until the end of the int, but does not read the newline character after the int. So, the first iteration displays "enter the name", then calls nextLine() which reads the end of the line where you typed the number of players (an empty string). Then the second iteration starts and displays "enter the name", and nextLine() blocks until you type a newline character.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533520

You are reading three lines. The problem you have is that nextInt() reads an int value, it doesn't read and consume the end of the line. (A common mistake)

You need the nextLine() after it to say that you want to ignore the rest of the line.

Upvotes: 3

Mark Peters
Mark Peters

Reputation: 81074

When you use Scanner.nextInt(), it does not consume the new line (or other delimiter) itself so the next token returned will typically be an empty string. Thus, you need to follow it with a Scanner.nextLine(). You can discard the result instead of assigning it to a[0]:

int n = sc.nextInt(); 
sc.nextLine();

It's for this reason that I suggest always using nextLine (or BufferedReader.readLine()) and doing the parsing after using Integer.parseInt().

Upvotes: 6

Related Questions