Ava does not have powerful functions like scanf in C, mostly by defining input and output stream objects.
The commonly used classes have bufferedreader,scanner.
Instance Program:
First, using Scanner to read from the keyboard integer or float type data
Java code
ImportJava.util.*; Public classTest { Public Static voidMain (String args[]) {Scanner in=NewScanner (system.in);//defining an object using the Scanner classSYSTEM.OUT.PRINTLN ("Please input a float number"); floatA=in.nextfloat ();//receive float type dataSystem.out.println (a); System.out.println ("Please input a integer number"); intB=in.nextint ();//Receive shaping DataSystem.out.println (b); } }
Second, using BufferedReader to read the string from the keyboard and write into the file Abc.txt
Java code
ImportJava.io.*; Public classTest { Public Static voidMain (string[] args)throwsIOException {bufferedreader buf=NewBufferedReader (NewInputStreamReader (system.in)); BufferedWriter Buff=NewBufferedWriter (NewFileWriter ("Abc.txt")); String Str=Buf.readline (); while(!str.equals ("Exit") ) {buff.write (str); Buff.newline (); STR=Buf.readline (); } buf.close (); Buff.close (); } }
Notes on the JDK1.5 scanner class
Scanner is a new class that SDK1.5 adds, but uses this class to create an object.
Scanner reader=new Scanner (system.in);
The reader object then calls the following methods (functions) to read the various data types that the user entered at the command line:
Next. Byte (), nextdouble (), Nextfloat,nextint (), nextline (), Nextlong (), Nextshot ()
Use the Nextline () method to enter a line that may contain spaces. If you read a word, you can call the
The. Next () method
Iii. the difference between scanner and BufferedReader
When entering data into a program in command-line mode, we can use the standard input string object system.in. However, we do not use it directly, because the Read method provided by System.in can only read one byte of data at a time, What we usually use is to read a string or a number, so the functionality provided by the Read method is not of much use to us.
In Java SE 6, you can use the Scanner class to get the user's input, Scanner class is located in the Java.util package, if you want to use Scanner to get user input, add import java.util.Scanner; This statement. The function of import is to tell the compiler that you will use the scanner class in the Java.util package.
Let's look at an example:
Java code
import Java.util.Scanner; public class Test { public static void main (string[] args) {Scanner scan = new Scanner (system.in); System.out.println ( "Please enter a string:" "The string you entered is:" + Scan.next ()); } }
Run the above program and you will see that the string you entered will be displayed as shown below.
Let's take a look at the meaning of each statement in this program:
New is to create an object, in which new means to create an object scan of the scanner class. But when you create an object of the scanner class, you need to use system.in as its argument, or you can think of scanner as a supporter of System.in objects. System.in get the user input content, give scanner to do some processing.
Several methods are available in the Scanner class:
Next (): Gets a string;
Nextint (): Converts the obtained string to an integer of type int;
Nextfloat (): Converts the obtained string into float type;
Nextboolean (): Converts the obtained string to a Boolean type;
Using scanner to get the user's input is very convenient, but the scanner gets input based on the space character, including the SPACEBAR, TAB key, and enter key. When you press either of these keys, scanner returns the next input. When you include spaces in the middle of your input, it is clear that using scanner will not fully get the string you entered. At this point we can consider using the BufferedReader class to get input. In fact, in Java SE 1.4 and previous versions, there is no scanner method available, We also use Bufferreader when we get the input.
The BufferedReader class is located in the Java.io package, so to use this class, it is necessary to introduce java.io this package: Import Java.io.BufferedReader.
The ReadLine () method that uses the BufferedReader object must handle the java.io.IOException exception (Exception).
Using BufferedReader to get input is much more complicated to understand. But using this method is fixed, and it is possible to do so 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, not including the last pressed enter return character.
The complete sample program is as follows:
Java code
ImportJava.io.BufferedReader; Importjava.io.IOException; ImportJava.io.InputStreamReader; Public classTestbufferedreader { Public Static voidMain (string[] args)throwsIOException {BufferedReader buffer=NewBufferedReader (NewInputStreamReader (system.in)); System.out.println ("Please enter a string of strings"); String text=Buffer.readline (); System.out.println ("The string you entered is:" +text); } }
Four, using System.in.read to realize from the keyboard read into Char type data
Java code
SYSTEM.OUT.PRINTLN ("Please input a char"); Char c= (char) System.in.read ();
How to use the keyboard to enter a number, character, and string (go) in Java