(1)读取控制台输入
java的控制台输入由System.in
完成。
为了获得一个绑定到控制台的字符流,你可以把System.in
包装在一个BufferedReader对象中创建一个字符流。
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
实例化BufferedReader类时需要传入一个IO对象。
BufferedReader对象创建后,我们便可以使用如下方法:
- 用
read()
方法从控制台读取一个字符;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int charASCII = br.read();//这里读取的是输入的字符的ASCII码值
char inputChar = (char) charASCII; //强制类型转换为字符类型
System.out.println(inputChar+":"+charASCII);//打印出输入的字符和其ASCII码值
因为调用了read()方法,所以要抛出IOException,即在包含上述代码的方法外部要
throws IOException
,即声明IO异常;
- 用
readLine()
方法读取一个字符串;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine(); //读取一个从控制台输入的字符串
System.out.println(inputString);
这里也是如此,方法外部也需要声明IO异常(
throws IOException
);
(2)从控制台读取多字符输入
public static void IOTest() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ascii;
char c;
// 定义一个StringBuilder对象用来动态存储输入的字符
StringBuilder str = new StringBuilder();
System.out.println("输入字符,直到按下空格结束:");
while (true){
ascii = br.read(); //这里读取的是输入的字符的ASCII码值,即int类型的值
// 当读取到输入的字符为换行符的时候跳出循环
if (ascii == 10) //换行符的ASCII码值为10
break;
c = (char)ascii;
str.append(c);
}
System.out.println(str);
}
运行结果如下:
输入字符,直到按下空格结束:
Believe yourself!
Believe yourself!
(3)从控制台读取多字符串
public static void IOTest() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder res = new StringBuilder();//用来存储输入的字符行
System.out.println("请输入字符行,直到输入‘end’结束输入:");
String str;
while (true){
str = br.readLine();
if (str.equals("end")) //当输入end的时候,跳出循环
break;
res.append(str);
}
System.out.println(res.toString());
}
运行结果如下:
请输入字符行,直到输入‘end’结束输入:
You can fail,
but you can't lose your confidence!
end
You can fail,but you can't lose your confidence!
(4)通过Scanner读取控制台输入
我们可以通过Scanner类获取用户的输入
Scanner scanner = new Scanner(System.in);
通过Scanner类的next()
与nextLine()
方法获取输入的字符串,在读取前一般需要使用hasNext()
与hasNextLine()
判断是否还有输入的数据;
next()
读取键盘输入的字符串并打印,直到用户输入字符串“end”为止。
public static void scannerTest(){
Scanner scanner = new Scanner(System.in);//接收键盘上输入的数据
System.out.println("next方式读取数据:");
while (scanner.hasNext()){
//当读取到输入的字符串是“end”,表示输入结束。
String str = scanner.next();
if (str.equals("end"))
break;
System.out.println("输入的数据为:"+str); // (1)
}
scanner.close();//关闭scanner对象
}
运行结果为:
(1)
next方式读取数据:
Hello, how are you ! end
输入的数据为:Hello,
输入的数据为:how
输入的数据为:are
输入的数据为:you
输入的数据为:!
Process finished with exit code 0
(2)
next方式读取数据:
Hello, How are you !
输入的数据为:Hello,
输入的数据为:How
输入的数据为:are
输入的数据为:you
输入的数据为:!
end
Process finished with exit code 0
将(1)处的输出语句改为如下形式,则:
System.out.print(str+" ");
其运行结果如下:
next方式读取数据:
Hello, how are you ! end
Hello, how are you!
Process finished with exit code 0
- (1)next()方法默认将空格作为字符串的分隔符,所以不能读取带有空格的字符串。
例如:h ello,这里是分为两个字符串,所以是读取两次,h 一次,ello 一次;
- (2)next()方法可以修改默认的字符串分隔符;
// 上面对应的代码改成如下所示
Scanner scanner = new Scanner(System.in);
// 使用userDelimiter("_")将默认的字符串分隔符改为“_”(下划线)
String str = scanner.useDelimiter("_").next();
则运行结果为:
next方式读取数据:
Hello_How_are_you_!_end
Hello How are you !
- (3)对输入有效字符之前的空白,next()方法会自动将其去掉;
nextLine()
nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions thecursor in the next line.
读取整行的数据包括单词间的空格,到回车结束(也就是从开始读一整行包括回车),读取结束后,光标放在下一行开头。
public static void scannerTest1(){
Scanner scanner = new Scanner(System.in);
System.out.println("nextLine方式读取数据:");
while (scanner.hasNextLine()){
String str = scanner.nextLine();
if (str.equals("end"))
break;
System.out.println("输出的数据为: "+str);
}
scanner.close();
}
运行结果:
nextLine方式读取数据:
Hello, How are you ! end
输出的数据为: Hello, How are you ! end
Nice to meet you! end
输出的数据为: Nice to meet you! end
end
注意: 我们在输入“Hello,How are you ! end”后并没有结束输入,因为nextLine()读取的是输入回车键之前的所有字符,所以这里
String str = scanner.nextLine()
读取的是“Hello,How are you ! end”,即str = "Hello,How are you ! end"
,而不是str = "end"
,所以这里不会终此输入。
而下面的单独一行输入“end”之后, 才结束输入。
- (1)以Enter(回车键)为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符;
- (2)可以获取空白(读取空白);
nextInt()
nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.
只读取整数型数据, nextInt()在读取完输入后把光标放在读取数据的同一行,该数据的后面。
public static void scannerTest2(){
Scanner scanner = new Scanner(System.in);
System.out.println("nextInt方式读取数据:");
while (scanner.hasNextInt()){ //只要输入的数据不是整型数据,立马结束输入的读取
int data = scanner.nextInt();
System.out.println("输出的数据为: "+data);
}
scanner.close();
}
nextInt方式读取数据:
23 89 299 09 78 jksdf
输出的数据为: 23
输出的数据为: 89
输出的数据为: 299
输出的数据为: 9
输出的数据为: 78
Process finished with exit code 0
只要输入的不是整型数据(除分隔符以外),则结束输入。
nextDouble()
与nextInt()方法类似,不同的是只读取小数(double)类型的数据。
参考: