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

When entering java, how to enter to end the input

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "java input through enter to end the input", in the daily operation, I believe that many people in the java input through the enter to end the input problem there are doubts, the editor looked up all kinds of data, sorted out a simple and useful method of operation, hope to answer the "java input through the enter to end the input" doubt! Next, please follow the editor to study!

When typing, enter (enter) to end the input

When writing java programs, we want to type enter to complete the input of this line, this is a very common problem, but if we are using Scanner, and then through the nextInt () method call, will not stop typing, back to type a blank line, and then wait for you to enter the next number.

Solution.

We can set two Scanner to read the data in behavior units first, which is equivalent to using enter as the Terminator.

Then pass the read string into the second Scanner, and then process it

ArrayList arr = new ArrayList (); System.out.println ("Enter a space separated list of numbers:"); Scanner in = new Scanner (System.in); String line = in.nextLine (); Scanner in2 = new Scanner (line); while (in2.hasNextInt ()) {arr.add (in2.nextInt ());} System.out.println ("The numbers were:" + arr.toString ())

Just change functions such as nextInt or hasnextint to the function name you need to achieve different inputs, but it ends with enter.

Enter one line at the end of java enter

Title

Find this way in the forum:

Scanner s = new Scanner (System.in); String str= ""; do {str=s.nextLine (); if (s.hasNextLine ()) break;} while (true); System.out.println (str)

After practice, this method does not work:

Two carriage returns are required to end the input.

It is found that the solution Scanner s = new Scanner (System.in); String str=s.nextLine (); Scanner st=new Scanner (str); ArrayList a = new ArrayList (); while (st.hasNextInt ()) a.add (st.nextInt ()); System.out.println (a)

Use the string as the input stream and the space as the spacer:

Source code:

This calls the following constructor:

At this point, on the "java input how to enter through the enter to end the input," the study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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