-5

I am making a program and am using user input. When I am getting a String I have always used .nextLine(), but I have also used .next() and it does the same thing. What is the difference?

Scanner input = new Scanner(System.in);
String h = input.nextLine();
String n = input.next();

What is the difference?

1 Answers1

0

To make long story short:

nextLine - reads the whole line.

next - reads until the first space.

As an example:

// Input : Barack Obama
String st = in.next(); // st holds the String "Barack"
String st2 = in.nextLine(); //st2 holds the String "Barack Obama"
Name
  • 11
  • I was also using them in a for loop and noticed that if you where entering a number before the String then it would skip past the nextLine and it would not skip past the next. Why is this? – TravisTeague01 Mar 07 '16 at 00:08