2

I am trying to write a for loop that will allow a user to input five names and ages. The problem I am having is that it runs right the first time but then when it runs the second time the first name and last name are combined and it is not allowing two string entries. It will allow one then move to the integer entry for age. Any help is appreciated.

import java.util.Scanner;

public class QueueImp extends people{

    public static void main(String[] args) {
        String userL;
        String userF;
        int userA;
        int userchoice = 0;

        Scanner scnr = new Scanner(System.in);

        for(int i = 0; i<5; i++) {      
        System.out.print("Please enter last name: ");
        userL = scnr.nextLine();
        System.out.println("");
        System.out.println("Please enter first name: ");
        userF = scnr.nextLine();
        System.out.println("Please enter age: ");
        userA = scnr.nextInt();
        System.out.println("");
        pName(userL, userF, userA);
 }
AsthaUndefined
  • 1,067
  • 1
  • 10
  • 21

3 Answers3

1

The reason for that is the newline character (from inputting the integer) is not consumed yet. So, it is consumed by scnr.nextLine() of the next loop iteration, hence skipping the second last name input. To consume the newline character, add 1 more scnr.nextLine() right after scanning the integer.

for(int i = 0; i<5; i++) {      
    System.out.print("Please enter last name: ");
    userL = scnr.nextLine();
    System.out.println("");
    System.out.println("Please enter first name: ");
    userF = scnr.nextLine();
    System.out.println("Please enter age: ");
    userA = scnr.nextInt();
    System.out.println("");
    scnr.nextLine();
    pName(userL, userF, userA);
}
Andreas
  • 2,300
  • 10
  • 19
  • 23
1

Please use the next insted of nextLine

System.out.print("Please enter last name: ");
userL = scnr.next();

next() can read the input only till the space. It can't read two words separated by a space. Also, next() places the cursor in the same line after reading the input.

@Edit: nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

For reading the entire line you can use nextLine().

For reference: What's the difference between next() and nextLine() methods from Scanner class?

If you want to use the same than please use like this to consume the new line character by Integer.parseInt

userA = Integer.parseInt(scnr.nextInt());
SaviNuclear
  • 882
  • 6
  • 19
0

Using scnr.next() will solve your problem.

Why it wasn't working with scnr.nextLine() is because next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line. The reason you were not able to enter the 2nd iteration of details is that the pointer already moved ahead before the 2nd iteration itself.

System.out.print("Please enter last name: ");
userL = scnr.next();
System.out.println("Please enter first name: ");
userF = scnr.next();
System.out.println("Please enter age: ");
userA = scnr.nextInt();
System.out.println(userL + " " + userF + " " + userA);
AsthaUndefined
  • 1,067
  • 1
  • 10
  • 21