Scanner - 从文本文件中读取字符串、整数、双精度数
Scanner - Reading Strings, Ints, Doubles from a text File
我正在做一个简单的销售报告程序,它从一个文本文件中读取并将其分配给一个数组
文本文件包含:
Coke
34 3.00 5.00
Sprite
18 2.89 4.30
Fanta Mango
21 3.35 5.99
Orange Crush
35 4.00 6.00
Browns Cream Soda
43 4.25 5.75
当我使用扫描仪读取每一行并将其分配给数组时,问题就出现了。
我的代码如下所示:
while(in.hasNext()) {
sodaBrand[sodaBrand.length - 1]= in.nextLine();
numberOfBottles[numberOfBottles.length -1] = in.nextInt();
costOfBottles[costOfBottles.length - 1] = in.nextDouble();
retailCost[retailCost.length - 1] = in.nextDouble();
}
如我所料,它给了我一个输入不匹配异常,因为它将文件中的所有文本都视为字符串。
关于从哪里开始解决这个问题有什么想法吗?任何帮助将不胜感激。
谢谢
我在我的记事本中复制了你的文件。让我在您的文件中添加 \n 以获得更好的解释。 你的文件不是这样的。这只是为了更清楚。
这里的\n表示移动到下一行
Coke\n
34 3.00 5.00\n
Sprite\n
18 2.89 4.30\n
Fanta Mango\n
21 3.35 5.99\n
Orange Crush\n
35 4.00 6.00\n
Browns Cream Soda\n
43 4.25 5.75
我在记事本中复制它时注意到了这一点。你文件的最后一行最后没有\n。
我还将给出您在代码中使用的各种方法的语法。访问此处了解有关扫描仪的更多信息。 https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
boolean hasNext()
//Returns true if this scanner has another token in its input.
int nextInt()
//Scans the next token of the input as an int.
double nextDouble()
//Scans the next token of the input as a double.
String nextLine()
//Advances this scanner past the current line and returns the input that was
//skipped.
现在让我们看看代码中的每一行都发生了什么。
这是带有注释的代码。
while(in.hasNext()) //returns true as Coke is available as a token for input
{
sodaBrand[sodaBrand.length - 1]= in.nextLine(); //reads Coke and moves the cursor to next line
numberOfBottles[numberOfBottles.length -1] = in.nextInt(); //reads 34
costOfBottles[costOfBottles.length - 1] = in.nextDouble(); //reads 3.00
retailCost[retailCost.length - 1] = in.nextDouble(); //reads 5.00
}
注意最后输入in.nextDouble()后光标还没有移动到下一行。
现在这个时候while条件被选中,就会发生这种情况,
while(in.hasNext()) //returns true as it has Sprite as String available for input
但是正如我所说你的光标还在这一行的\n之前
34 3.00 5.00\n
下一次调用 in.nextLine() 会做的是 return "" 并将光标移动到下一行 Sprite\n.
现在对 in.nextInt() 的下一次调用会导致 InputMismatchException,因为它有 Sprite 可用于不是 int 的输入。
将您的代码更改为此。
while(in.hasNext())
{
sodaBrand[sodaBrand.length - 1]= in.nextLine();
numberOfBottles[numberOfBottles.length -1] = in.nextInt();
costOfBottles[costOfBottles.length - 1] = in.nextDouble();
retailCost[retailCost.length - 1] = in.nextDouble();
in.nextLine(); //this will cause the scanner to return "" and move the control to the next line
}
您的代码在读取所有行后仍将给出 NoSuchElementException。这样做是因为您的文本文件的最后一行没有 \n。
转到您的文件并将光标移动到最后一行。您会看到光标的最后位置在 5.75 之后。如果按下向下箭头,光标将不会移动。
这显示了您的文件在此处结束的内容。末尾没有回车或\n。
按回车键并保存文件。您现在将看到最后一个光标位置在 5.75 之后的下一行中 。 现在您的文件到此结束。您的文件现在变成了。
Coke\n
34 3.00 5.00\n
Sprite\n
18 2.89 4.30\n
Fanta Mango\n
21 3.35 5.99\n
Orange Crush\n
35 4.00 6.00\n
Browns Cream Soda\n
43 4.25 5.75\n
毕竟,您的代码应该可以正常工作。
最后一条建议。我自己是初学者,扫描仪并不像看起来那么简单。它的 nextInt() next() nextLine() 和各种方法看似相似,其实不然。这会导致难以调试的错误。尝试使用 BufferedReader。
43 4.25 5.75\n
希望对你有所帮助
在最后一行retailCost[retailCost.length - 1] = in.nextDouble();
之后,需要多一个in.nextLine();
考虑下面的示例文本文件:
1234
word
这等同于:
1234.56\nword
其中 \n 表示换行符。 nextDouble() 或任何其他采用数字输入的 Scanner 方法将 return 下一个可用数字。 nextLine() returns 从当前位置到下一行的所有数据。
考虑这段代码(其中 'in' 是一个 Scanner 对象,上面的文本文件作为输入流):
double a = sc.nextDouble(); //remaining input stream: \nword
String b = sc.nextLine(); //remaining input stream: word
String c = sc.nextLine();
可以看出,在第二行中,扫描仪对象立即遇到一个表示行结束的'\n'。因此在执行结束时,值为:
a = 1234.56
b = ""
c = "word"
我正在做一个简单的销售报告程序,它从一个文本文件中读取并将其分配给一个数组
文本文件包含:
Coke
34 3.00 5.00
Sprite
18 2.89 4.30
Fanta Mango
21 3.35 5.99
Orange Crush
35 4.00 6.00
Browns Cream Soda
43 4.25 5.75
当我使用扫描仪读取每一行并将其分配给数组时,问题就出现了。 我的代码如下所示:
while(in.hasNext()) {
sodaBrand[sodaBrand.length - 1]= in.nextLine();
numberOfBottles[numberOfBottles.length -1] = in.nextInt();
costOfBottles[costOfBottles.length - 1] = in.nextDouble();
retailCost[retailCost.length - 1] = in.nextDouble();
}
如我所料,它给了我一个输入不匹配异常,因为它将文件中的所有文本都视为字符串。
关于从哪里开始解决这个问题有什么想法吗?任何帮助将不胜感激。
谢谢
我在我的记事本中复制了你的文件。让我在您的文件中添加 \n 以获得更好的解释。 你的文件不是这样的。这只是为了更清楚。 这里的\n表示移动到下一行
Coke\n
34 3.00 5.00\n
Sprite\n
18 2.89 4.30\n
Fanta Mango\n
21 3.35 5.99\n
Orange Crush\n
35 4.00 6.00\n
Browns Cream Soda\n
43 4.25 5.75
我在记事本中复制它时注意到了这一点。你文件的最后一行最后没有\n。
我还将给出您在代码中使用的各种方法的语法。访问此处了解有关扫描仪的更多信息。 https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
boolean hasNext()
//Returns true if this scanner has another token in its input.
int nextInt()
//Scans the next token of the input as an int.
double nextDouble()
//Scans the next token of the input as a double.
String nextLine()
//Advances this scanner past the current line and returns the input that was
//skipped.
现在让我们看看代码中的每一行都发生了什么。
这是带有注释的代码。
while(in.hasNext()) //returns true as Coke is available as a token for input
{
sodaBrand[sodaBrand.length - 1]= in.nextLine(); //reads Coke and moves the cursor to next line
numberOfBottles[numberOfBottles.length -1] = in.nextInt(); //reads 34
costOfBottles[costOfBottles.length - 1] = in.nextDouble(); //reads 3.00
retailCost[retailCost.length - 1] = in.nextDouble(); //reads 5.00
}
注意最后输入in.nextDouble()后光标还没有移动到下一行。
现在这个时候while条件被选中,就会发生这种情况,
while(in.hasNext()) //returns true as it has Sprite as String available for input
但是正如我所说你的光标还在这一行的\n之前
34 3.00 5.00\n
下一次调用 in.nextLine() 会做的是 return "" 并将光标移动到下一行 Sprite\n.
现在对 in.nextInt() 的下一次调用会导致 InputMismatchException,因为它有 Sprite 可用于不是 int 的输入。
将您的代码更改为此。
while(in.hasNext())
{
sodaBrand[sodaBrand.length - 1]= in.nextLine();
numberOfBottles[numberOfBottles.length -1] = in.nextInt();
costOfBottles[costOfBottles.length - 1] = in.nextDouble();
retailCost[retailCost.length - 1] = in.nextDouble();
in.nextLine(); //this will cause the scanner to return "" and move the control to the next line
}
您的代码在读取所有行后仍将给出 NoSuchElementException。这样做是因为您的文本文件的最后一行没有 \n。
转到您的文件并将光标移动到最后一行。您会看到光标的最后位置在 5.75 之后。如果按下向下箭头,光标将不会移动。 这显示了您的文件在此处结束的内容。末尾没有回车或\n。
按回车键并保存文件。您现在将看到最后一个光标位置在 5.75 之后的下一行中 。 现在您的文件到此结束。您的文件现在变成了。
Coke\n
34 3.00 5.00\n
Sprite\n
18 2.89 4.30\n
Fanta Mango\n
21 3.35 5.99\n
Orange Crush\n
35 4.00 6.00\n
Browns Cream Soda\n
43 4.25 5.75\n
毕竟,您的代码应该可以正常工作。
最后一条建议。我自己是初学者,扫描仪并不像看起来那么简单。它的 nextInt() next() nextLine() 和各种方法看似相似,其实不然。这会导致难以调试的错误。尝试使用 BufferedReader。
43 4.25 5.75\n
希望对你有所帮助
在最后一行retailCost[retailCost.length - 1] = in.nextDouble();
之后,需要多一个in.nextLine();
考虑下面的示例文本文件:
1234
word
这等同于:
1234.56\nword
其中 \n 表示换行符。 nextDouble() 或任何其他采用数字输入的 Scanner 方法将 return 下一个可用数字。 nextLine() returns 从当前位置到下一行的所有数据。 考虑这段代码(其中 'in' 是一个 Scanner 对象,上面的文本文件作为输入流):
double a = sc.nextDouble(); //remaining input stream: \nword
String b = sc.nextLine(); //remaining input stream: word
String c = sc.nextLine();
可以看出,在第二行中,扫描仪对象立即遇到一个表示行结束的'\n'。因此在执行结束时,值为:
a = 1234.56
b = ""
c = "word"