In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve the problem of java input blank line end". In daily operation, I believe that many people have doubts about how to solve the problem of java input blank line end problem. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer "how to solve the problem of java input blank line end problem". Next, please follow the editor to study!
At the end of the java input blank line, the last two written tests have been plagued by this problem.
You have tried various ways to stop typing after entering a blank line. Here is the code that can do this:
Public static void main (String [] args) {Scanner in = new Scanner (System.in); while (true) {/ / first String s = in.nextLine (); if (s.equals (")) / / second break;System.out.println (s);} System.out.println (" Over Input ");}
The first judgment condition in the above code can be replaced with in.hasNextLine (), but not in.hasNext (). The second judgment condition can be s.isEmpty () or s.length = = 0.
I looked up the information and found:
Next () differs from nextLine in the following ways:
Next () must read valid characters before the end of the input, the next () method will automatically remove the space bar, Tab key or enter key and other terminators encountered before entering valid characters, only after entering valid characters, the next () method will enter the space bar, Tab key or enter key as a delimiter or Terminator.
Simply put, the next method cannot get a string with spaces.
The Terminator of the nextLine () method is just the enter key, that is, the nextLine () method returns all the characters before the enter key, and it can get a string with spaces.
Java input data, space to continue, enter at the end of the ordinary version
It can be input and output. With detailed notes
Import java.util.Scanner;public class SumDemo {public static void main (String [] args) {System.out.println ("Please enter two digits separated by spaces, such as 55"); / / get a scanner to scan the system's input Scanner input = new Scanner (System.in); / / declare a temporary string variable temp to hold the line read by the scanner String temp = input.nextLine (); / / temp string first trim (), that is, to remove the white space on both sides, / / because some people may enter spaces 5 spaces 5 spaces enter enter. . / / so removing the spaces on both sides and turning into 5 spaces and 5 carriage returns meets the requirements. The / / split ("") method indicates that the string is cut with spaces, and the result is a string array String [] ss = temp.trim (). Split ("); / / parse two numbers from two strings and sum int num1 = Integer.parseInt (ss [0]). Int num2 = Integer.parseInt (ss [1]); int sum = num1+num2; / / output result System.out.println (the sum of "input number is + num1+"+ num2+" is "+ sum"); / / form a good habit of keeping open resources closed. When we open the scanner, we should close the scanner input.close ();}} upgrade version
Can repeatedly input numbers, repeat output results, and with exit function,
Import java.util.Scanner;public class SumTest {public static void main (String [] args) {Scanner input = new Scanner (System.in); while (true) {System.out.println ("if you enter exit, exit. Enter two numbers separated by spaces "); String temp = input.nextLine (); if (temp.trim (). Equals (" exit ")) {break;} String [] ss = temp.trim (). Split ("); int num1 = Integer.parseInt (ss [0]); int num2 = Integer.parseInt (ss [1]) Int sum = num1+num2; System.out.println ("the number entered is" + num1+ "" + num2+ "the sum of two numbers is:" + sum);} input.close ();}} at this point, the study on "how to solve the problem of java input blank line ending" is over, hoping to solve everyone's 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.