Java console input summary

Source: Internet
Author: User

1. How to read and output a data or array from the console

Input a number and Output2Medium Method

1) bufferedreader BR =NewBufferedreader (NewInputstreamreader (system.In); // Read one
Number

String
STR = Br. Readline ();

IntI = integer.Parseint(STR );

System.Out. Print (I );

 

2)Byte[] B =New Byte[10]; // another way to read one number

System.In. Read (B );

String
S =NewString (B );

IntJ = integer.Parseint(S. Trim ());

System.Out. Println (s );

System.Out. Println (j );

 

Input an array and output it:

Bufferedreader Br2 =NewBufferedreader (New
Inputstreamreader (system.In));

IntA [] =New Int[10]; // a way to read array and print

For(IntK = 0; k <A. length; k ++ ){

A [k] = integer.Parseint(Br2.readline ());

}

For(IntK = 0; k <A. length; k ++ ){

System.Out. Println (A [k]);

}

In this process, you must note that these codes must capture exceptions!

 

 

 

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.