1

I am trying to input some text using a scanner but I am having an issue where the output for name is skipping and going to the next line. If I try to input name it will skip and go straight to surname. Can anyone please assist

Scanner input = new Scanner(System.in);
            if(input.hasNext("1")){   
            System.out.print("Input your Name: ");
            name = input.next();
            System.out.print("Input your Surname: ");
            surname = input.next();
            System.out.print("Input your ID: ");
            id = input.nextInt();
            System.out.print("Input the training program: ");
            trainingProgramme = input.next();
            registerApplicant(name, surname, id, trainingProgramme);
            System.out.print("Congratulations! You have registered for the " +trainingProgramme+ " Training Programme");
            }

The output:

debug:
Welcome to the Codex Registration system: 
Please select an option: 

1 Register for a training program 
2 Check registration details 
3 Exit program 
1
Input your Name: Input your Surname: Blah
Input your ID: 6787
Input the training program: Java
Congratulations! You have registered for the Java Training ProgrammeWelcome to the Codex Registration system: 
Please select an option: 
Progman
  • 16,827
  • 6
  • 33
  • 48
Anesu Mazvimavi
  • 131
  • 1
  • 9

3 Answers3

2

The problem is, that with input.hasNext("1") you are not skipping the input 1 from the user.

So the input 1 will be stored to your variable name.

Therefore you need to skip this input. One possibility is to put a input.next() before reading the name like this:

    Scanner input = new Scanner(System.in);
    if(input.hasNext("1")){
      input.next(); //skip the input of choice
      System.out.print("Input your Name: ");
      name = input.next();
      System.out.print("Input your Surname: ");
      surname = input.next();
      System.out.print("Input your ID: ");
      id = input.nextInt();
      System.out.print("Input the training program: ");
      trainingProgramme = input.next();
      registerApplicant(name, surname, id, trainingProgramme);
      System.out.print("Congratulations! You have registered for the " +trainingProgramme+ " Training Programme");
    }

or (imho the better way) you use a variable for the choice-input:

    Scanner input = new Scanner(System.in);

    String selection = input.next();

    if("1".equals(selection)){
        //...
    }
csalmhof
  • 1,820
  • 2
  • 15
  • 24
1

The problem is that a new line character added to end of the every scanner-

You can manually set the delimiter of the Scanner:

 scanner = new Scanner(...).useDelimiter(" "); //To use only space as a delimiter.
E P
  • 389
  • 4
  • 16
1

Try to use a new variable for choice.

Scanner input = new Scanner(System.in);

String choice = input.next();

if (choice =="1") {   
    System.out.print("Input your Name: ");
    name = input.next();
    
    System.out.print("Input your Surname: ");
    surname = input.next();
    
    System.out.print("Input your ID: ");
    id = input.nextInt();
    
    System.out.print("Input the training program: ");
    trainingProgramme = input.next();
    
    registerApplicant(name, surname, id, trainingProgramme);
    System.out.print("Congratulations! You have registered for the " +trainingProgramme+ " Training Programme");
}
hc_dev
  • 8,389
  • 1
  • 26
  • 38
Jin Thakur
  • 2,711
  • 18
  • 15