1

I am trying to read some data for a "Question" type, which has an id(int), statement(string) and answer(string). I'am using the code below:

Scanner scanner = new Scanner(System.in);
System.out.print("Id (uniquely!): ");
int id = scanner.nextInt();
System.out.print("Statement : ");
String statement = scanner.next();
System.out.print("Answer:  ");
String answer = scanner.next();

If I enter sth like this "Who are you?" for "statement", it doesn't wait to type anything else for "answer" too. But if I do not use spaces in my statement it will work just fine. Also, if I use scanner.nextLine(), instead of scanner.next(), it doesn't work properly; it will allow me to introduce only one string for both statement and answer.

Does anyone have any idea?

Bruce P
  • 17,554
  • 7
  • 59
  • 69
Nelly
  • 301
  • 3
  • 5
  • 14
  • Could you change your code to make it clearer what "statement" and "answer" are? I assume `stat` and `ras` but doing so would be nice for the non-whatever-language-that-is users here. – Thomas Nov 20 '15 at 13:24
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Codebender Nov 20 '15 at 13:25
  • You 've to use nextline() method. For reasons why it's not working fine for you, look at the above link.. – Codebender Nov 20 '15 at 13:26
  • @ Thomas, sorry 'bout that :D I changed that – Nelly Nov 20 '15 at 13:33
  • @Codebender, all the solutions from that link are suggesting to use "nextLine()", which doesn't help me very much in this case.. – Nelly Nov 20 '15 at 13:35
  • Please add an example to your question of how you implemented `nextLine()` and what your input/output was, versus what you expected. – Monkpit Nov 20 '15 at 13:54

5 Answers5

2

try add scanner.nextLine(). It happens because scanner.nextInt() read only that number and not whole line, so rest of that line is still there. And you want to get rid of it so use nextLine() to get on next line.

It may look like this:

Scanner scanner = new Scanner(System.in);
System.out.print("Id (unic!): ");
int id = scanner.nextInt();
//Consume rest of the line
scanner.nextLine();
System.out.print("Enunt : ");
String stat = scanner.next();
System.out.print("Raspuns:  ");
String ras = scanner.next();
edasssus
  • 321
  • 4
  • 15
1

Change to :

String statement = scanner.nextLine();     
String answer = scanner.nextLine();
instead of using scanner.next();

Basically when you want trying to input string with spaces then try to use scanner.nextLine() instead of using scanner.next()

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

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.

Shiladittya Chakraborty
  • 3,982
  • 7
  • 35
  • 74
0

Here scanner.next() takes token without space. You may try scanner.nextLine()

Razib
  • 10,057
  • 10
  • 46
  • 71
0

The reason your code was not working is :that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).

So you have to implement a nextLine() twice to get your output. As showed below.

Scanner scanner = new Scanner(System.in);
System.out.print("Id (uniquely!): ");
int id = scanner.nextInt();
System.out.print("Statement : ");
scanner.nextLine();
String statement = scanner.nextLine();
System.out.print("Answer:  ");
String answer = scanner.nextLine();
0

or You can Scan a dummy String variable before scanning the original String Variable as shown below:

Scanner scanner = new Scanner(System.in);
System.out.print("Id (uniquely!): ");
String k = ""; //Dummy String Variable
int id = scanner.nextInt();
System.out.print("Statement : ");
k = scanner.nextLine(); // scanning of Dummy variable
String statement = scanner.nextLine();
System.out.print("Answer:  ");
String answer = scanner.nextLine();