Java -- Method for retrieving data input from the console, java Console

Source: Internet
Author: User

Java -- Method for retrieving data input from the console, java Console

1. Use the standard input string System. in

System. in. read (); // read-only data in one byte at a time, but we often want to obtain a string or a group of numbers.

 

2. Use numeric to obtain a string or a group of numbers

// The next () method of substring is used to obtain the user input string; nextInt () converts the obtained input string to the integer type; similarly, nextFloat () is converted to the floating point type; nextBoolean () convert to boolean.

// Enter space, tab, and enter as the Terminator;

System. out. print ("input n :");
Repeated scan = new partition (System. in );
String read = scan. nextLine ();
Int n = Integer. parseInt (read );
System. out. println ("input data n =" + n );

 

3. Use BufferedReader to obtain the input with spaces

// To obtain the input containing spaces, use the java. io. BufferedReader class;

BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));
String read1 = null;
System. out. print ("input data :");
Try {
Read1 = br. readLine ();
} Catch (IOException e ){
E. printStackTrace ();
}
System. out. println ("input data:" + read1 );
String [] split_num = read1.split ("");
Int [] split_num2int = new int [n];
For (int I = 0; I <split_num.length; I ++ ){
System. out. print (split_num [I] + "");
Split_num2int [I] = Integer. parseInt (split_num [I]);

}


Related Article

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.