I'm new to this Java world, and just can't figure out this problem. After I use nextInt or nextDouble, and try to use nextLine, it won't work unless I put nextLine twice. What's the problem there, and what would be the good way to handle this?

Just gave a code to illustrate a simple example I can understand.

import java.util.Scanner;

public class nextLineProblem {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Type in something for line 1");
		String line1 = sc.nextLine();

		System.out.println("Type an integer for line 2");
		int line2 = sc.nextInt();
		System.out.println("Type in something for line 3");
		String line3 = sc.nextLine();
		System.out.println("Line1: " + line1 + "\n" + "Line2: " + line2 + "\n" + "Line3:" + line3);


The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline.

Smart explanation. Couldn't find it.

Thanks, now I understand what's going on now and how to deal with it when using the Scanner class.
Just left pondering if it's a bug or a feature :)

A new problem came to mind today.
It's easy to make a way around this problem if I know I'm going to have it, but what if it's unknown that I'll have a nextLine() after nextInt() , how would I be able to deal with the problem then?
For instance if there's a random function involved. Would I just have to scrap the nextLine() and go with something else?

Check out the documentation for the scanner class.

It will throw "NoSuchElementException" if no line was found. If you don't haven't learned about exception handling yet, I would use "hasNext".

int myInt = 0;
String myString = "";

Scanner in = new Scanner(System.in);

if (in.hasNextInt()){
     //read in int value
     myInt = in.nextInt();
else if (in.hasNext()){
     //read in String value
     myString = in.next();
}//else if

No, haven't learned about exception handling yet, but I'll read up on that now. Thanks for pointing it out.

THANKS SO MUCH FOR POSTING!!! I encountered the same problem and was getting so frustrated with the program not performing like I expected it to.
Sure enough, putting in nextLine() twice solved the problem.
I think that was is happening is that the nextInt and nextDouble are not properly terminating the allocated line of memory, so that when the nextLine() is run it is actually terminating the line that already has a value in it -- entered from the previous nextInt or nextDouble -- rather than taking in a new string readline value.

I am new to programming too,however, i saw some flaws on it

you dont put String line1,line2line3;

that is why the above codes don't work becs. its missing something.

I just share my knowledge as a beginner in java too..

i have try some practice and found that whatever nextline() you hava,only the last one can't use.and i don't understand that.

It's a real problem with Scanner's design - so many people fall into this trap.
You have some input with an int followed by some text, eg

John Doe

... and you try to read it with

int num = scanner.nextInt();
String name = scanner.nextLine();

... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:
1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.

Hi All,

for this problem, you have to call a function and give the input in the method.
see my program:

import java.util.*;

public class ScannerDemo

public static void main(String[] args)

 Scanner scanner = new Scanner(System.in);
 ScannerDemo x = new ScannerDemo();
 int i= scanner.nextInt();
 double d= scanner.nextDouble();

 System.out.println("Double is: "+ d);
 System.out.println("Int value is "+ i);


 void show()
 Scanner sc = new Scanner(System.in);
 String s=sc.nextLine();
 System.out.println("string is: " + s);



Absolute rubbish. Does not address actual question. 2 years too late.


Welcome to DaniWeb. Just let me point out a few things:

  1. When using code blocks, put the entire code in them, it makes it clearer
  2. Don't just post code, it doesn't explain anything. Rather answer with an explanation of how to get to the correct code, or what is wrong in the current code, so the OP is able to solve it for him/herself.
  3. If you do post code, add some comments in it, to explain where you did what, and why you did certain things
  4. This question was marked as 'solved' 6 years ago. There was no reason to revive this thread, unless you disagree with the previously provided solutions, but, if that's the case, you should add some information about what it is you disagree with, and why, or why your solution is better.
This question has already been answered. Start a new discussion instead.