3

When I run my program instead of reading the string and storing it in tempAddress my program simply prints the next line before I enter input. Using next works for the first two because I am only using one word but the third one encompasses multiple words so need something else and through my research I found nextLine() was the answer but I am not able to get it to work as others have, thanks in advance.

System.out.println("Enter Employee First Name: ");
String tempFirstName = input.next();
employeesArray[i].setFirstName(tempFirstName);

System.out.println("Enter Employee Last Name: ");
String tempLastName = input.next();
employeesArray[i].setLastName(tempLastName);

System.out.println("Enter Employee Address: ");
String tempAddress = input.nextLine();
employeesArray[i].setAddress(tempAddress);

System.out.println("Enter Employee Title: ");
String tempTitle = input.next();
employeesArray[i].setTitle(tempTitle);
enkrates
  • 606
  • 1
  • 6
  • 17
hoss24
  • 366
  • 7
  • 21

2 Answers2

2

Basically Scanner tokenizes the input by default using whitespace. Using next() method of scanner returns the first token before the space and the pointer stays there. Using nextLine() returns the whole line and then moves the pointer to the next line.

The reason your nextLine() was not behaving fine was because, your previous input for employee last name using next() cause the pointer to stay in the line hence, when you reach the point to take the employee address using nextLine(), the pointer returns remainder of the previous input next() which was obviously empty (when supplied one word as input to next()). Assume you entered two words separated by space for last name, the next() will store the first word in last name field and pointer waits after first token before second token and as soon as you reach nextLine() pointer returns the second token and moves to new line.

The solution is to execute nextLine() after reading the input for last name to make sure that your pointer is in new line waiting for input for address.

I updated my code by inserting a input.nextLine() there to make sure that scanner input is consumed and pointer is moved to the next line.

    System.out.println("Enter Employee First Name: ");
    String tempFirstName = input.next();
    employeesArray[i].setFirstName(tempFirstName);

    System.out.println("Enter Employee Last Name: ");
    String tempLastName = input.next();
    employeesArray[i].setLastName(tempLastName);

    //feed this to move the scanner to next line
    input.nextLine(); 

    System.out.println("Enter Employee Address: ");
    String tempAddress = input.nextLine();
    employeesArray[i].setAddress(tempAddress);

    System.out.println("Enter Employee Title: ");
    String tempTitle = input.next();
    employeesArray[i].setTitle(tempTitle);
Raf
  • 6,620
  • 1
  • 35
  • 54
  • Please add some explanation. – tungd Nov 19 '15 at 01:58
  • 1
    this works, but what if you don't want to have ' ' (space) in your input? you would have to make a whole thing just for that. – 789 Nov 19 '15 at 02:01
  • I have updated the answer, as well as update the code with a fix that addresses his issue. @789 and tungd cheers for pointing out the issue. – Raf Nov 19 '15 at 02:25
  • @789 yes, provided last name is always one token - which is in nature. nextLine() will move it to the new line, regardless. – Raf Nov 19 '15 at 02:29
  • @user2383106 glad it helped. upvote the question as it was useful and I learned from answering it ;) – Raf Nov 19 '15 at 17:37
1

When you have input.next(), it reads the input, but not the newline character, it leaves it in the input stream. input.nextLine() ends with a newline character. So when input.nextLine() is executed, it stops without taking any input because it already got the newline (\n) char from the input stream.

Solution: read the newline before you execute inupt.nextLine():

System.out.println("Enter Employee Address: ");
input.next();//read the newline char - and don't store it, we don't need it.
String tempAddress = input.nextLine();

see also: https://stackoverflow.com/a/13102066/3013996

Community
  • 1
  • 1
789
  • 678
  • 1
  • 9
  • 29
  • Thanks you for your help, I have accepted other answer as it was first, but made sure to upvote your answer and comments – hoss24 Nov 19 '15 at 17:29