Java read a string type variable from the keyboard

zhaozj2021-02-12  175

See a question in the Netease Forum Java version:

Topic: How to write a string type variable from the keyboard?

Re: How to write from the keyboard from the keyboard to write a string variable?

DataInputStream

Write you with a simple example, let's take a look:

(From the keyboard input, determine if the input is Chinese)

Import java.io. *; class strhz {public static void main (string args []) {dataputstream is = new datainputstream (system.in); try {string str = is.readline (); byte a [] = Str.getbytes (); if (a [0]> 0) System.out.Println ("You Enter is Chinese"); Else System.Println ("You Enter is Not Chinese");} catch (Exception E ) {}}}

In recent days, studying the stream in Java, it is really unbearable.

Remember DataInputStream no readline () method.

I checked the manual to get the following conclusion:

Well, the readline () method of DataInputStream is not advocated.

You should use the bufferedReader readline () method instead of it.

It should be like this,

BufferedReader Br = New BufferedReader (NEW INPUTSTREADER (System.in);

String str = br.readline ();

JDK Reference Manual:

Readline ()

Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine () method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader Class by Replacing Code of The Form:

DataInputStream D = New DataInputStream (in);

With:

BufferedReader D

= New BufferedReader (New InputStreamReader (in);

转载请注明原文地址:https://www.9cbs.com/read-6524.html

New Post(0)