java扫描器从文本文件中读取字符串、int和double

oewdyzsn  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(256)

我正在做一个简单的销售报告程序,从一个文本文件读取并分配给一个数组
文本文件包含:

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();

            }

正如我所期望的,它会给我一个输入不匹配的异常,因为它将文件中的所有文本都视为字符串。
有什么办法可以开始解决这个问题吗?任何帮助都将不胜感激。
谢谢

q43xntqr

q43xntqr1#

我把你的文件抄在记事本里了。请允许我在您的文件中添加\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(in.hasNext())  //returns true as it has Sprite as String available for input

但正如我所说的,您的光标仍在\n这一行之前

34 3.00 5.00\n

对in.nextline()的下一个调用将返回“”并将光标移动到下一行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。
按enter键并保存文件。现在您将看到最后一个光标位置位于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

我希望我帮助过你。

k75qkfdt

k75qkfdt2#

在最后一行之后 retailCost[retailCost.length - 1] = in.nextDouble(); ,需要额外的 in.nextLine(); 考虑下面的示例文本文件:

1234
word

这与:

1234.56\nword

其中\n表示换行符。nextdouble()或任何其他扫描器方法获取一个数字输入将返回下一个可用的数字。nextline()返回从当前位置到下一行的所有数据。考虑以下代码(其中“in”是以上述文本文件作为输入流的scanner对象):

double a = sc.nextDouble(); //remaining input stream: \nword
String b = sc.nextLine(); //remaining input stream: word
String c = sc.nextLine();

可以看到,在第二行中,scanner对象立即遇到一个“\n”,它表示行的结束。因此,在执行结束时,值为:

a = 1234.56
b = ""
c = "word"

相关问题