Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the situation that input is skipped in Java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Java input is skipped how to solve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this Java input is skipped how to solve the article will have a harvest, let's take a look at it.

Question code import java.util.Scanner;public class Main {public static void main (String [] args) {Scanner reader = new Scanner (System.in); System.out.print ("Please enter:"); double a = reader.nextDouble (); System.out.print ("Please enter:") / / the input of this sentence is skipped ↓ String s = reader.nextLine ();}}

At run time, it is found that the second input is skipped directly

To solve this problem, I explored the situation where input was skipped in Java

Come to a conclusion first

There are two inputs in succession in ① Java, and in some cases the second input is skipped.

② input is skipped only if the nextLine () method occurs

③ when the nextLine (); method is placed after any input method (nextXXX ()) (excluding nextLine ()), the nextLine () method skips the input because it reads the carriage return

④ 's simple solution: replace nextLine (); with next () or write two nextLine (), and let the first nextLine () eat and enter.

Ⅰ. Input of two numbers:

Enter two data of type int

Scanner reader = new Scanner (System.in); System.out.print ("enter number:"); int a = reader.nextInt (); System.out.print ("enter number:"); a = reader.nextInt ()

You can find that there is no problem with the input. By the same token, double,float,Long,short, byte data can be input normally

Ⅱ. Input of two strings:

1. Two next (); and two nextLine ()

System.out.print ("input string:"); String s = reader.next (); System.out.print ("input string:"); s = reader.next ()

There is no problem with input.

two。 First nextLine () and then next ()

System.out.print ("input string:"); String s = reader.nextLine (); System.out.print ("input string:"); s = reader.next ()

The running result is the same as above, no problem

After adding the loop

While (true) {System.out.print ("input string:"); String s = reader.nextLine (); System.out.print ("input string:"); s = reader.next ();}

After adding the loop, it is found that the input of nextLine () has been skipped

At the same time, we find that after adding the loop, the input of nextLine (); becomes after next (), which may be the reason why the input is skipped.

3. First next () and then nextLine ()

System.out.print ("input string:"); String s = reader.next (); System.out.print ("input string:"); s = reader.nextLine ()

Found that the second input was skipped directly.

Ⅲ. The input of a number and a string

1. Number before string

Case 1: string input with nextLine ()

System.out.print ("input number:"); int a = reader.nextInt (); System.out.print ("input string:"); String s = reader.nextLine ()

Found that the second input was skipped

It is concluded that digital input can not be connected to nextLine (); input

Case 2: string input with next ()

System.out.print ("input number:"); int a = reader.nextInt (); System.out.print ("input string:"); String s = reader.next ()

two。 String before number

Case 1: string input with nextLine ()

Can be entered normally

Join the loop

While (true) {System.out.print ("input string:"); String s = reader.nextLine (); System.out.print ("enter number:"); int a = reader.nextInt ();}

It is found that after adding the loop, the input of nextLine (); is skipped as the result of nextLine () followed by next ();.

It is speculated that after adding the loop, the input of nextLine (); becomes after next (), * * reads the carriage return of the previous input, so the input is skipped.

Case 2: string input with next ()

System.out.print ("input string:"); String s = reader.next (); System.out.print ("enter number:"); int a = reader.nextInt ()

If you add a loop, you can also enter it normally.

It can be found that using next (); the input string has no effect either before or after the number input

While (true) {System.out.print ("input string:"); String s = reader.next (); System.out.print ("enter number:"); int a = reader.nextInt ();}

To sum up, it is found that when the nextLine () method is located after any input, nextLine () will read the carriage return of the previous input, resulting in the situation that the input of nextLine () is skipped.

The next () method does not have this problem in inputting a string.

Note: the test shows that "" does not become the returned character, which means that the string that should have been entered with the nextLine () method is empty, not "".

This is the end of the article on "how to solve the situation in which input is skipped in Java". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to solve the situation in which input is skipped in Java". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report