The use of scanner class Next and Nextline method

Source: Internet
Author: User
Tags first string

The key to the use of nextline () and Next () is that when the next () method encounters the first valid character (not a space, newline character), the scan begins, and when the first delimiter or terminator (space or line break) is met, the scan ends. , then use Nextline () to continue reading, it is possible to read the first character is a space or line break.      I personally prefer to use the scanner scanner when implementing the character window input, which is easier to operate. In the process of writing, I found that there are two ways to implement string input using scanner, one is next () and one is nextline (), but what is the difference between these two methods?     I looked up some information to summarize the following:    next () must be read to a valid character before you can end the input, to enter a valid character before the space bar, Tab, or enter key, such as The Terminator, the next () method will automatically remove it, The next () method treats the input space bar, tab, or enter key as delimiters or terminators only after the valid characters are entered. To put it simply, next () finds and returns the next complete tag from this scanner. Before and after the complete tag is the input information with the delimited pattern, so 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 characters before the ENTER key, which is a string that can be given a space.   Example:    In view of the differences between the two methods, students must pay attention to the next () method and the Nextline () method used, for example, as follows:    package Com.tarena.corejava; import Java.util.scanner;public class TestScanner2 {public static void main (string[] args) { String s1,s2; Scanner  SC = new Scanner (system.in); System.out.println ("Please enter the first string:");//s1 = Sc.nextline (); S1=sc.next (); System.out.println ("Please enter a second character:");//s2 = Sc.next (); S2=sc.nextline (); SystEm.out.println ("Input string is:" +s1+ "" "+s2);}} As you can see, nextline () automatically reads the Enter that was removed by next () as his terminator, so there is no way to give S2 keyboard verification, I found other next methods, such as Dobul nextdouble (), float next float (), int This problem exists when nextint () and nextline () are in use, and the workaround is to add a nextint () statement after the first next (), Nextdouble (), Nextfloat (), nextline (), and then the next () The removed enter terminator is filtered out, for example the above program is rewritten as: public static void Main (string[] args) {String s1,s2; Scanner  SC = new Scanner (system.in); System.out.println ("Please enter first string:");//s1 = Sc.nextline (); S1=sc.next (); Sc.nextline (); System.out.println ("Please enter a second character:");//s2 = Sc.next (); S2=sc.nextline (); System.out.println ("Input string is:" +s1+ "" +s2);} You can do it.   Example Two: Package Com.tarena.corejava; import Java.util.scanner;public class Testscanner {public static void main (string[] args) {Scanner scan = new Scanner (system.in); System.out.println ("Test Next: Input string:"); String s2 = Scan.next (); SYSTEM.OUT.PRINTLN ("The resulting string is:" +S2); System.out.println ("Test nextline: Input string:"); String s3 = Scan.nextline (); SYSTEM.OUT.PRINTLN ("The resulting string is:" +s3+ "length is:" +s3.length ());}}  

Note When next () is used with nextline ():

Next () when used before nextline, next scans for spaces or characters before line breaks, does not read "\ n", and Nextline starts reading from "\ n", so no data,

Correct method:

    1. int n = cin.nextint ();
    2. while (str = Cin.nextline (). Equles (""))
    3. {
    4. }
Original address: http://blog.sina.com.cn/s/blog_81547cad01018mnd.html

The use of scanner class Next and Nextline method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.