While using next() or nextfoo() , the upcoming nextLine() is skipped by scanner

0 votes
I am using "nextInt()" and "nextLine()" for reading input. The problem comes that after entering numerical value first "input.nextLine()" is skipped. Help me to  implement this.
Jun 6, 2018 in Java by sridhar
• 160 points
169 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

That's because the Scanner.nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine.

You will encounter the similar behaviour when you use "Scanner.nextLine" after Scanner.next() or any "Scanner.nextFoo" method.

you read the input through Scanner.nextLine and convert your input to the proper format you need. For examples, to an integer using "Integer.parseInt(String)" method.
int option = 0;
try {
    option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
String str1 = input.nextLine();

answered Jun 6, 2018 by mitto
• 160 points

Related Questions In Java

0 votes
1 answer

Why is the Scanner skipping nextLine() after using next() or nextFoo()

public static void main(String[] args) { ...READ MORE

answered Aug 27, 2018 in Java by samarth295
• 2,190 points
65 views
0 votes
2 answers

Scanner is skipping nextLine() after using next() or nextFoo()?

use input.nextLine(); after your nextInt() function for example:- input.nextInt(); ...READ MORE

answered May 11 in Java by Sourav Dangi
440 views
0 votes
1 answer

Is Java “pass-by-reference” or “pass-by-value”?

Think of reference parameters as being aliases ...READ MORE

answered Jun 8, 2018 in Java by Rishabh
• 3,520 points
35 views
0 votes
1 answer

Check whether the string is palindrome or not?

You may refer the below code: public static ...READ MORE

answered Jul 11, 2018 in Java by Akrati
• 3,150 points
45 views
0 votes
1 answer

How to find even or odd using call by value?

Call by value is, when a primitive ...READ MORE

answered Nov 23, 2018 in Java by Namitha
38 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
99 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
27 views
0 votes
1 answer

How do I get the current date and time using Java?

To print current date and time using ...READ MORE

answered Apr 13, 2018 in Java by Akrati
• 3,150 points
39 views
0 votes
3 answers

How to read input from Console using Scanner Class?

A simple example: import java.util.Scanner; public class Expl { ...READ MORE

answered Jul 31, 2018 in Java by samarth295
• 2,190 points
133 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,190 points
350 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.