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
3,085 views

1 answer to this question.

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 28, 2018 in Java by samarth295
• 2,220 points
1,736 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, 2019 in Java by Sourav Dangi
18,029 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,620 points
1,363 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,190 points
1,086 views
+1 vote
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
1,527 views
0 votes
1 answer

Is there a way to mask the password from java user input that comes from Scanner class and System.out.Println()?

You can use the console class to do so. ...READ MORE

answered May 27, 2019 in Java by Omkar
• 69,220 points
11,126 views
0 votes
0 answers

Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?

I'm attempting to provide authorisation using JavaScript by connecting to the RESTful API integrated into Flask.  However, when I make the request, I receive the following error: XMLHttpRequest cannot load http://myApiUrl/login. No 'Access-Control-Allow-Origin' header ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
550 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,220 points
3,615 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,220 points
1,039 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP