In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to solve the end-of-input tag problem of Scanner object". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to solve the end-of-input tag problem of Scanner object" can help you solve the problem.
Input end tag for Scanner object
Recently began to review the java, I do not know or need to pay attention to take notes
Package gt; import java.util.Scanner; / * Created by Cronous on 2017-10-29. * / public class day01 {public static void main (String [] args) {System.out.print ("Please enter value:"); Scanner s = new Scanner (System.in); while (s.hasNext ()) {System.out.println (s.next ());}
We enter "123 345 678", then enter and find that the console prints.
one hundred and twenty three
three hundred and forty five
six hundred and seventy eight
I took a look at scanner's api and it said:
Scanner uses the delimiter pattern to decompose its input into tags, which by default matches whitespace.
You can then use different next methods to convert the resulting tags to different types of values.
So here we know that it matches with white space by default, so there is a space in our input, and it will be read once until we enter, but you will find that there is no space after our last number, and the system splits the data.
Maybe I don't know what I'm talking about here. Take a look at the next example:
Package gt; import java.util.Scanner; / * Created by Cronous on 2017-10-29. * / public class day01 {public static void main (String [] args) {System.out.print ("Please enter a value:"); Scanner s = new Scanner (System.in); s.useDelimiter (""); / / here we actively set the system delimiter to "" while (s.hasNext ()) {System.out.println (s.next ());}
Here, we actively set a delimiter "" and then enter data as we did last time. After typing, press enter and the console effect is as follows:
one hundred and twenty three
three hundred and forty five
Here we will find that the third number has no output. Why? We found that when we finished typing the last number, we pressed enter directly and did not enter the delimiter we set.
So the third number has not been printed out. If we enter "123 345 678" and then press enter, we can output three values normally, ensuring that there is a space at the end.
Look at another code (notice that there is a hole here). My requirement is to get the input value, and when the input value is "quit", we end the input.
Package gt; import java.util.Scanner; / * Created by Cronous on 2017-10-29. * / public class day01 {public static void main (String [] args) {System.out.print ("Please enter value:"); Scanner s = new Scanner (System.in); while (s.hasNext ()) {if ("quit" .equals (s.next () {break;} else {System.out.println (s.next ()) }
Here we enter "12345 quit" to confirm, here we want to get the output as
one hundred and twenty three
three hundred and forty five
But the output we get is
three hundred and forty five
Why is there only "345" output? Here I find a problem through debug: because when we judge whether it is equal to the "quit" value, we have already got "123" by calling s.next (), and then print the value obtained again, that is, the next value" 345 ", so finally there is the problem of printing only" 345 ".
The correct code is given below, we just need to make sure that the value of s.next () is uniform.
Package gt; import java.util.Scanner; / * Created by Cronous on 2017-10-29. * / public class day01 {public static void main (String [] args) {System.out.print ("Please enter a value:"); Scanner s = new Scanner (System.in); while (s.hasNext ()) {String a = s.next (); / / assign s.next () to the variable an if ("quit" .equals (a)) {break } else {System.out.println (a);}
This can be solved, the problem is not big, but need to pay attention to.
Input and understanding of Scanner Scanner
The meaning of new freshman
System.out output
System.in input
Scannery object
Basic syntax:
Scanner s = new Scanner (System.in)
Get the input string through the next (next) and nextline (next line) methods of the Scanner class. We usually need to use hasNext () and hasNextline () to determine whether there is any input data before reading it.
Package Scnner;import java.util.Scanner;public class Day01 {public static void main (String [] args) {/ / create a scan object to receive keyboard data Scanner scanner = new Scanner (System.in) / / receive the user's input and encapsulate it with scanner System.out.println ("receive with next:") / / determine whether the user has output string if (scanner.hasNext () = = true) {/ / default = = true here, you are welcome, but it is recommended to write String str = scanner.next () / / use next to receive System.out.println ("the output is:" + str);}
Next ()
You must read valid characters before you can end the input.
The next () method automatically removes whitespace encountered before entering valid characters.
Only after entering a valid character do you use the white space entered after it as a delimiter or lodging character.
Next () cannot get a string with spaces
NextLine ()
With enter as the Terminator, that is, all the characters before the return of the input returned by the nextLine () method.
You can get blank.
Package Scnner;import java.util.Scanner;public class Day02 {public static void main (String [] args) {/ / receive data from the keyboard Scanner scanner = new Scanner (System.in); System.out.println ("receive with nextLine:") / / determine whether there is any input if (scanner.hasNextLine ()) {/ / waiting for the user to enter String str = scanner.nextLine () System.out.println ("output content:" + str);} scanner.close ();}}
Of course, there is no need for if to judge.
If (scanner.hasNextLine ()) package Scnner;import java.util.Scanner;public class Day03 {public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please input data:"); String str = scanner.nextLine (); System.out.println ("output:" + str); scanner.close ();}
This is also possible.
Understanding of Scanner input content (key points)
Personally, I understand it this way.
Input and output, we just need to remember three lines of code.
System.out.println ("Please enter data:"); String str = scanner.nextLine (); System.out.println ("output content:" + str)
The first line of code is the title
Please enter data:
The second line of code: is all the output of our keyboard, until you press the enter key (enter), all the content into a variable to save in str
The third line of code: wait for the second line of code to finish, and display the output + str (all the characters you typed on the keyboard, that is, the content) together.
The figure below is as follows
First behavior, first line of code.
The second line of hello world is the second line of code, which is what you typed.
The third line is to wait for the second line of code to finish, and the output underwear + all the characters typed by the keyboard will be displayed together.
Note / / all classes that are IO streams will always take up resources if they are not closed, so turn off scanner.close () if you want to form a good habit of using it; / / IO stream input and output stream on "how to solve the problem of input end tags of Scanner objects" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.