Java:使用 BufferedReader 读取输入时出错
Java : Error when reading input with BufferedReader
我正在与 Java 合作来解释一些输入。我正在使用 BufferedReader。我的目标是在读取一个字符后读取一定数量的行,其中 -1 是停止命令。像这样
2
CASE 1 - L1
CASE 1 - L2
3
CASE 2 - L1
CASE 2 - L2
CASE 2 - L3
-1
我的目标是输出:
CASE 1 - L1
CASE 1 - L2
CASE 2 - L1
CASE 2 - L2
CASE 2 - L3
这意味着我以正确的方式获取台词。我的代码如下:
public class TESTE {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int state;
while ((state = buffer.read()) != 45) {
char c = (char) state;
if (Character.isDigit(c)) {
int n = Character.getNumericValue(c);
for (int i = 0; i < n; ++i) {
System.out.println("L:" + buffer.readLine());
System.out.println();
}
}
}
}
}
出于某种原因,我的输出是:
L:
L:CASE 1 - L1
L: - L2
L:
L:CASE 2 - L1
L:CASE 2 - L2
L: - L3
我做错了什么?有没有一种优雅的方式来处理该输入?
在你的 for
循环中你有 buffer.readline()
。因此,您实际上是一次阅读了所有行(而我认为您希望逐行阅读)。
您可能希望利用功能 readLine()
来简化您的代码,例如。它 returns null
当到达输出末尾时。这将允许您消除有点笨拙的 -1
指示输入结束。
String line;
while ((line = buffer.readLine()) != null) {
if (!Character.isDigit(line.charAt(0)) {
System.out.println("L:" + line);
}
}
一个全面的答案如下,可以满足您的目的。请注意,与其考虑单个数字,不如考虑整数,因为该值可以大于 9。在这种情况下,考虑单个字符是不够的!
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = buffer.readLine()) != null) {
int value;
try {
value = Integer.parseInt(line); // if user gives non-integer values as input
} catch (NumberFormatException ex) {
System.out.println("Error!");
continue; // print error message and then continue
}
if (value == -1) {
break;
}
for (int i = 0; i < value; i++) {
line = buffer.readLine(); // keep asking for input from user
System.out.println("L:" + line);
}
}
}
事情是这样的。
buffer.read()
returns 50
即 '2'
- 转换为
n
,即2
- 读取两行
此时缓冲区中的下一步是什么? '2'
之后的 \n
!这就是为什么你得到第一个输出
L:
空行。
好吧,没有必要继续了。整个过程都断了。
一种方法:
Scanner in = new Scanner(System.in);
while (true) {
int n = Integer.parseInt(in.nextLine());
if (n == -1) break;
for (int i = 0; i < n; i++) {
System.out.println("L:" + in.nextLine());
}
}
我正在与 Java 合作来解释一些输入。我正在使用 BufferedReader。我的目标是在读取一个字符后读取一定数量的行,其中 -1 是停止命令。像这样
2
CASE 1 - L1
CASE 1 - L2
3
CASE 2 - L1
CASE 2 - L2
CASE 2 - L3
-1
我的目标是输出:
CASE 1 - L1
CASE 1 - L2
CASE 2 - L1
CASE 2 - L2
CASE 2 - L3
这意味着我以正确的方式获取台词。我的代码如下:
public class TESTE {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int state;
while ((state = buffer.read()) != 45) {
char c = (char) state;
if (Character.isDigit(c)) {
int n = Character.getNumericValue(c);
for (int i = 0; i < n; ++i) {
System.out.println("L:" + buffer.readLine());
System.out.println();
}
}
}
}
}
出于某种原因,我的输出是:
L:
L:CASE 1 - L1
L: - L2
L:
L:CASE 2 - L1
L:CASE 2 - L2
L: - L3
我做错了什么?有没有一种优雅的方式来处理该输入?
在你的 for
循环中你有 buffer.readline()
。因此,您实际上是一次阅读了所有行(而我认为您希望逐行阅读)。
您可能希望利用功能 readLine()
来简化您的代码,例如。它 returns null
当到达输出末尾时。这将允许您消除有点笨拙的 -1
指示输入结束。
String line;
while ((line = buffer.readLine()) != null) {
if (!Character.isDigit(line.charAt(0)) {
System.out.println("L:" + line);
}
}
一个全面的答案如下,可以满足您的目的。请注意,与其考虑单个数字,不如考虑整数,因为该值可以大于 9。在这种情况下,考虑单个字符是不够的!
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = buffer.readLine()) != null) {
int value;
try {
value = Integer.parseInt(line); // if user gives non-integer values as input
} catch (NumberFormatException ex) {
System.out.println("Error!");
continue; // print error message and then continue
}
if (value == -1) {
break;
}
for (int i = 0; i < value; i++) {
line = buffer.readLine(); // keep asking for input from user
System.out.println("L:" + line);
}
}
}
事情是这样的。
buffer.read()
returns50
即'2'
- 转换为
n
,即2
- 读取两行
此时缓冲区中的下一步是什么? '2'
之后的 \n
!这就是为什么你得到第一个输出
L:
空行。
好吧,没有必要继续了。整个过程都断了。
一种方法:
Scanner in = new Scanner(System.in);
while (true) {
int n = Integer.parseInt(in.nextLine());
if (n == -1) break;
for (int i = 0; i < n; i++) {
System.out.println("L:" + in.nextLine());
}
}