簡體   English   中英

掃描儀 - 從文本文件中讀取字符串、整數、雙精度數

[英]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。

我還將提供您在代碼中使用的各種方法的語法。 訪問此處了解有關 Scanner 的更多信息。 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() 將返回 ""並將光標移動到下一行,即 Sprite\\n。

現在對 in.nextInt() 的下一次調用會導致 InputMismatchException,因為它具有可用於不是 int 的輸入的 Sprite。

將您的代碼更改為此。

    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 方法將返回下一個可用數字。 nextLine() 返回從當前位置到下一行的所有數據。 考慮這個代碼(其中 '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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM