如何从Java中的键盘获取String对象?
问题描述:
例如,我想获得一个"HelloWorld!"从Java键盘中获得,我将不胜感激
for example,I want to get a "HelloWorld!" from the keyboard in Java,I will appreciate
if you can show the code.
答
这是一个简单的代码段,请阅读键盘,直到按下Enter键为止.
Here is a simple snippet, reading the keyboard until the Enter-button is hit.
import java.io.*;
class MyClass
{
public static void main(String[] args) throws IOException{
//define String for input
String Read= "";
//instantiate InputStreamReader for Textstream
InputStreamReader Reader= new InputStreamReader(System.in);
//instantiate BufferedReader for buffering the stream
BufferedReader BufferedIn= new BufferedReader(Reader);
//while Enter is not hit, the input stream is read from keyboard
Read= BufferedIn.readLine();
}
}
}