Java basics: 17th Basic Input and Output

Source: Internet
Author: User

 

Input and output are the ways in which applications interact with users. In the command line mode, standard input and output devices (the console of the keyboard and display) are used ), in Windows applications, users can interact with users through graphical user interfaces, while in Web applications, users can interact with users through browsers and HTML languages. Currently, there are not many graphical user interfaces, but many web applications. When learning the Basic Java syntax, it is easier to use the command line input and the console output. This article introduces the basic input and output through the nameline method. Basic outputThe output mainly uses the system. Out. Print and system. Out. println methods, which have been used before. The difference between them is that the former does not wrap, while the latter outputs a new line. Can output any data type. Example: system. out. println (1111); system. out. println (true); system. out. println (2323.33); system. out. println ("string"); system. out. println ('A '); Basic InputThe following describes how to accept keyboard input. The most basic method is to use the system. In. Read () method, but each time this method receives a character, the return value of this method is the encoding of this character. For example, to receive an input, use the following code: temp = system. In. Read (); where temp is an int type variable. If you want to receive a number, such as 22, you need to process it yourself. The following example shows how to convert the input information to a number: [Example 1] package iotest; public class simpleiotest {/*** @ Param ARGs */public static void main (string [] ARGs) {system. out. println ("Enter five integers separated by spaces and press enter to end :"); try {// array used to save 5 numbers int A [] = new int [5]; int I = 0; int temp; while (true) {// obtain the input character temp = system. in. read (); If (char) temp = '') {// If a space is encountered, it indicates the next integer I ++;} else if (char) temp = '/R') {// line break ends break;} else {// The input characters are constructed into numbers a [I] = A [I] * 10 + integer. parseint (string. valueof (char) temp); }}for (INT element: a) {// cyclically outputs 5 numbers: system. out. println (element) ;}} catch (exception e) {system. out. println ("exception:" + E. tostring () ;}}this method is very troublesome. The following describes two common methods. Use bufferedreaderBufferedreader provides the basic input stream system. in is encapsulated. You can read a row of data each time and then analyze the data. The following example provides the same functions as the preceding example, but the code is easier to understand, there are a lot of codes that do not understand. Remember the code first. [Example 2] package iotest; import Java. io. inputstreamreader; import Java. io. bufferedreader; public class bufferedreadertest {public static void main (string ARGs []) {system. out. println ("Enter five integers separated by spaces and press enter to end :"); try {// array used to save 5 numbers int A [] = new int [5]; // create the input stream object bufferedreader reader = new bufferedreader (New inputstreamreader (system. in); // read a row of Information string input = reader. readline (); // converts it to an array string [] numbers = input using space as the separator. split (""); // converts a string to a number and outputs for (INT I = 0; I <5; I ++) {// convert the string to a [I] = integer. parseint (numbers [I]); system. out. println (A [I]) ;}} catch (exception e) {system. out. println ("exception:" + E. tostring ());}}} Use LoggingThe use of bufferedreader simplifies the code, but it is not simple enough. The use of tranquility can simplify the code. The following code demonstrates usage. [Example 3] package iotest; import Java. util. required; public class scannertest {public static void main (string [] ARGs) {system. out. println ("Enter five integers separated by spaces and press enter to end:"); try {// create sequence Object Sequence sequence = new sequence (system. in); // array used to save 5 numbers int A [] = new int [5]; for (INT I = 0; I <5; I ++) {// convert the string to a [I] = digit. nextint (); system. out. println (A [I]) ;}} catch (exception e) {system. out. println ("exception:" + E. tostrin G () ;}} there are other next methods. If you are interested, refer to the JDK help documentation. Last time: Lecture 2 Use of date and timeNext time: 18th string usage (I)Li xucheng csdn blog: http://blog.csdn.net/javaeeteacher invites you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

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.