Multiple java keyboard input implementation methods

Source: Internet
Author: User
Tags float number java se

Instance program:
1. Use flock to read integer or float data from the keyboard

Copy codeThe Code is as follows: // import java. io .*;
Import java. util .*;
Public class InputTest {
Public static void main (String [] args ){
Callback in = new callback (System. in); // callback class
System. out. println ("Please input a float number :");
Float a = in. nextFloat (); // receives float data
System. out. println ("Please input a string:"); // The input String data is tried here, but it cannot be displayed with spaces in the middle. This function is not available in the examples class.
Reverse str = new reverse (System. in );
System. out. println ("The string is:" + str. next ());
System. out. println ("The float number is:" + );
For (int I = 0; I <4; I ++ ){
System. out. println ("Please input a int number:"); // for loop to receive int-type data
Int B = in. nextInt ();
System. out. println ("The int number is:" + B );
}
}
}

2. Use bufferedreaderto read the strings from the keyboard and write them into the file abc.txt.

Copy codeThe Code is as follows: import java. io .*;
Public class InputTest {
Public static void main (String [] args) throws IOException {
BufferedReader buf = new BufferedReader (new InputStreamReader (System. in ));
BufferedWriter buf2 = new BufferedWriter (new FileWriter ("abx.txt "));
String str = buf. readLine ();
While (! Str. equals ("exit ")){
Buf2.write (str );
Buf2.newLine ();
Str = buf. readLine ();
}
Buf. close ();
Buf2.close ();
}
}

About the JDK1.5 sequence class

Struct is a new class of SDK1.5, but an object is created using this class.
Internal reader = new external (System. in );

The reader object then calls the following methods (functions) to read various data types that the user inputs on the command line:
Next. Byte (), nextDouble (), nextFloat, nextInt (), nextLine (), nextLong (), nextShot ()
The input line using nextLine () may contain spaces. If a word is read, you can call the. next () method.

3. Differences between consumer and BufferedReader
To input data to a program in command line mode, we can use the standard input string object System. in. however, we do not often use it directly because System. the read method provided by in can only read one byte of data at a time. What we usually use is to read a string or a number. Therefore, the read method provides the following functions, it is not very useful for us.
In Java SE 6, you can use the inputs class to obtain user input. The inputs class is located in java. in the util package, if you want to use the token to obtain user input, add import java. util. statement. the import function tells the compiler that you will use java. the category class in the util package.
Let's look at an example:

Copy codeThe Code is as follows: import java. util. collections;
Public class testbench {
Public static void main (String [] args ){
Repeated scan = new partition (System. in );
System. out. println ("enter a string :");
System. out. println ("the string you entered is:" + scan. next ());
}
}

Run the above program and you will see that the string you entered will be displayed as is below.
Let's take a look at the meaning of each statement in this program:
New is to create an object. new in the program means to create a scan object of the category class. however, you must use System. as its parameter, in can also be considered as System. supporter of the in object, System. after obtaining the user input content, the in operator is handed over to the operator for some processing.
The Methods class provides multiple methods:
Next (): gets a string;
NextInt (): converts the obtained string to an integer of the int type;
NextFloat (): converts the obtained string to the float type;
NextBoolean (): converts the obtained string to the boolean type;

It is very convenient to use the delimiter to obtain user input, but the token is based on a space character, including the space key, Tab key, and Enter key. when you press any of these keys, the cursor will return the next input. when the content you enter contains spaces, it is clear that the string you entered cannot be completely obtained using the delimiter. at this time, we can consider using the BufferedReader class to obtain the input. in fact, in Java SE 1.4 and earlier versions, the callback method has not yet been provided. BufferReader is also used when we get the input.
The BufferedReader class is located in the java. io package. To use this class, we need to introduce the java. io package: import java. io. BufferedReader.
The readLine () method of the BufferedReader object must handle java. io. IOException (Exception ).
It is much more complicated to use BufferedReader to obtain input. However, this method is fixed and can be processed before each use.
BufferedReader buffer = new BufferedReader (new InputStreamReader (System. in ));
String text = buffer. readLine ();
The readLine () method returns all character input before the user presses the Enter key, excluding the return character of the last press Enter.
The complete example program is as follows:

Copy codeThe Code is as follows: import java. io. BufferedReader;
Public class TestBufferedReader {
Public static void main (String [] args) throws IOException {
BufferedReader buffer = new BufferedReader (new InputStreamReader (System. in ));
System. out. println ("enter a string ");
String text = buffer. readLine ();
System. out. println ("the string you entered is:" + text );
}

}

4. The following program is shown: class StringTest

Copy codeThe Code is as follows :{
Public static void main (String [] args)
{
System. out. println (args [0]);
System. out. println (args [1]);
System. out. println (args [2]);
}
}

The input content after the execution statement is java + class name will be received by args,
Because args receives command line parameters.

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.