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?

6
  • 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. Commented Nov 20, 2015 at 13:24
  • Possible duplicate of Skipping nextLine() after using next(), nextInt() or other nextFoo() methods Commented Nov 20, 2015 at 13:25
  • You 've to use nextline() method. For reasons why it's not working fine for you, look at the above link.. Commented Nov 20, 2015 at 13:26
  • @ Thomas, sorry 'bout that :D I changed that Commented Nov 20, 2015 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.. Commented Nov 20, 2015 at 13:35

5 Answers 5

4

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();
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments

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();

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.