In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use the hasNext () and next () methods in Java Scanner objects". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
HasNext () and next () methods in Scanner object
I believe many people like me have encountered this problem when they first came into contact with Java:
What we might want is to output "Please enter:" and then we can enter a string or number on the console.
However, the fact is that the console asks us to type before displaying "Please enter:".
What's going on?
Reason:
First, both sc.hasNext () and sc.next () can be used to enter
We can see that hasNext () returns the boolean type and next () returns the value you entered.
Sc.hasNext () can be understood as storing the value we entered into sc, while sc.next () can be understood as taking a value from sc and moving the identifier back after taking the value (it can be understood as: after taking the value, the value is no longer there). If there is no value in sc, it will also ask for a value (both of them can be entered earlier).
Then let's analyze the beginning of the program:
First of all, when we enter the while loop, sc.hasNext () asks us to enter a value for him, so it will not output "Please enter:". When we are finished, String str = sc.next () will take the value we entered from sc and return it to str. If there is no value, it will be blocked and asked us to give it a value.
Enter the difference between the hasNext () and hasNextLine () methods
Recently, I am learning to do some algorithm exercises on Niuke net.
It is found that in some cases, the written Java code works normally when the machine is running.
But it just can't pass the test case of Niuke net.
After checking and finally locating to the location where the test case is read, it is found that:
Scanner scanner = new Scanner (System.in); / / while (scanner.hasNextLine ()) {/ / TODO:implement code} / / the program returns to normal while (scanner.hasNext ()) {/ / TODO:implement code} when hasNextLine is modified to hasNext () method.
By searching the Internet, you can get the difference between the two methods:
When checking the input stream:
The hasNext () method determines whether there are any non-null characters next. If so, return true, otherwise return false
The hasNextLine () method determines whether there is a row (including blank lines) according to the row matching pattern, and if so, returns true, otherwise returns false
For example, we currently have the following test cases:
7 15 9 5
This test case is stored as a file on Niuke. Com.
On the linux system, there is a newline character at the end of the file\ n, which means that the data stream actually read from the System.in input stream looks like this:
7 15 9 5\ n
After the program has finished processing 5, there is only one newline character left in the input stream. After processing 5, while makes a loop judgment. At this time, the results of hasNext () method and hasNextLine () method are different.
The hasNext () method assumes that there are no more non-null characters after that and returns a false
The hasNextLine () method will think that the newline character\ nis a blank line and matches the matching pattern of the line, then it will return a true, but in fact, because there is no more data after that, an exception will occur when reading the input stream, resulting in an error in the whole operation.
Suggested scheme
If you use hasNextXxxx (), you will also use nextXxxx () later:
For example, if you use hasNextLine () before, then you will use nextLine () to process the input later.
If the nextInt () method is used later, then the hasNext () method should be used to determine.
That's all for "how to use the hasNext () and next () methods in the Java Scanner object. Thank you for reading." If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.