27

I got the following code:

int nnames;
String names[];

System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
names = new String[nnames];

for (int i = 0; i < names.length; i++){
  System.out.print("Type a name: ");
  names[i] = in.nextLine();
}

And the output for that code is the following:

How many names are you going to save:3 
Type a name: Type a name: John Doe
Type a name: John Lennon

Notice how it skipped the first name entry?? It skipped it and went straight for the second name entry. I have tried looking what causes this but I don't seem to be able to nail it. I hope someone can help me. Thanks

ivan.sim
  • 7,836
  • 5
  • 40
  • 57
marcoamorales
  • 595
  • 2
  • 8
  • 13
  • 2
    Exact (really, exact) match of http://stackoverflow.com/questions/1466008/java-scanner-class-reading-strings/ – CPerkins Sep 23 '09 at 19:35

5 Answers5

26

The reason for the error is that the nextInt only pulls the integer, not the newline. If you add a in.nextLine() before your for loop, it will eat the empty new line and allow you to enter 3 names.

int nnames;
String names[];

System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();

names = new String[nnames];
in.nextLine();
for (int i = 0; i < names.length; i++){
        System.out.print("Type a name: ");
        names[i] = in.nextLine();
}

or just read the line and parse the value as an Integer.

int nnames;
String names[];

System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = Integer.parseInt(in.nextLine().trim());

names = new String[nnames];
for (int i = 0; i < names.length; i++){
        System.out.print("Type a name: ");
        names[i] = in.nextLine();
}
ivan.sim
  • 7,836
  • 5
  • 40
  • 57
Joshua
  • 24,418
  • 22
  • 74
  • 106
6

use sc.nextLine(); two time so that we can read the last line of string

sc.nextLine() sc.nextLine()

Ankur Nirmalkar
  • 113
  • 1
  • 8
2

It's because the in.nextInt() doesn't change line. So you first "enter" (after you press 3 ) cause the endOfLine read by your in.nextLine() in your loop.

Here a small change that you can do:

int nnames;
    String names[];

    System.out.print("How many names are you going to save: ");
    Scanner in = new Scanner(System.in);
    nnames = Integer.parseInt(in.nextLine());
    names = new String[nnames];

    for (int i = 0; i < names.length; i++){
            System.out.print("Type a name: ");
            names[i] = in.nextLine();
    }
Nettogrof
  • 2,086
  • 2
  • 15
  • 22
2

This because in.nextInt() only receive a int number, doesn't receive a new line. So you input 3 and press "Enter", the end of line is read by in.nextline().

Here is my code:

int nnames;
String names[];

System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
in.nextLine();
names = new String[nnames];

for (int i = 0; i < names.length; i++){
        System.out.print("Type a name: ");
        names[i] = in.nextLine();
}
wangzhengyi
  • 836
  • 8
  • 8
0

You could have simply replaced

names[i] = in.nextLine(); with names[i] = in.next();

Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.

Angelin Nadar
  • 7,922
  • 8
  • 39
  • 51