Java Self-study note-Basic Syntax 2-receive input characters and scanner.nextline from the console

Source: Internet
Author: User

The Scanner class encapsulates the input stream object and gets the entire line of text characters entered by the user using the Nextline () method input stream

Package hahaa;

Import Java.util.Scanner;

public class Ha {
public static void Main (string[] args) {
Scanner Scanner =new Scanner (system.in);
System.out.println ("Please enter the number you want to enter");

String line = Scanner.nextline ();
System.out.println ("The length of the output number" +line.length ());

}

}

Control Desk

Please enter the number you want to enter
2315348
The length of the output number 7

Next () must be read to a valid character before the end of 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, only after entering a valid character, the next () method will be entered after the space bar, The TAB key or enter key is treated as a delimiter or terminator. Simply put, next () finds and returns the next complete tag from this scanner. Before and after the complete tag is the input information that matches the delimited pattern, so the next method cannot get a string with spaces. The Terminator of the Nextline () method is just the ENTER key, which means that the nextline () method returns all characters before the ENTER key, which is a string with spaces.

After each statement of next (), Nextdouble (), Nextfloat (), Nextint (), plus a nextline () statement, the next () will remove the space, including the space, until the text entered after enter is valid.

Main function (for reference)
public static void Main (string[] args) {

String s1,s2;
Scanner sc=new Scanner (system.in);
System.out.print ("Please enter the first string:");
S1=sc.next ();
Sc.nextline ();
System.out.print ("Please enter a second string:");
S2=sc.next ();
System.out.println ("Input string is:" +s1+ "" "+s2);
}

Console----

Please enter the first string: Woini DASDASFA
Please enter a second string: ADSDFSDASD
The string entered is: Woini Adsdfs

Java Self-study note-Basic Syntax 2-receive input characters and scanner.nextline from the console

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.