[英]InputMismatchException when using Scanner to to read from a file
[英]InputMismatchException while using Scanner to read from file
Java的新手。 我很難理解為什么我的代碼沒有運行。 當我嘗試運行代碼時,出現InputMismatchException異常。
我做了一些測試,如果文件中有空白,例如“ New York”,則會出現問題。 我一直在嘗試不同的事情,例如用.hasNextLine()而不是其他線程中建議的.hasnext()循環,但無濟於事。 有時,我可以讓它運行直到最后,它給了我NoSuchElementException。 如果可以的話,請把我帶到正確的方向,這將對您有所幫助!
import java.util.*;
import java.io.*;
public class StandaloneReport {
public static void main(String[] args) {
String fileInputName;
String fileOutputName;
String firstName;
String lastName;
String houseNumber;
String street;
String city;
String state;
String zip;
String productDescription;
double productPrice;
//Scanner obj1
Scanner input = null;
input = new Scanner(System.in);
System.out.printf("What is the file name?\n");
fileInputName = input.nextLine();
//Print out the name user inputed
System.out.println("File name is: " + fileInputName);
//Read the file
FileReader filereader;
Scanner readInput = null;
try {
readInput = new Scanner(filereader = new FileReader(fileInputName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (readInput.hasNext())
{
firstName = readInput.next();
lastName = readInput.next();
houseNumber = readInput.next();
street = readInput.next();
city = readInput.nextLine();
state = readInput.next();
zip = readInput.next();
productDescription = readInput.nextLine();
productPrice = readInput.nextDouble();
文本文件如下所示:
Jane
Doe
10
Broadway
New York
NY
10001
Galaxy S10
199.99
2
Samsung Bluetooth
29.99
1
Slim Fit Hard Plastic Case
2.99
2
Charger
17.99
3
錯誤我得到:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at hey.bcs.hwk.purchases.standalonereport.StandaloneReport.main(StandaloneReport.java:55)
我希望它能順利閱讀,因此可以在另一個文件中使用PrintStream進行打印,但我什至無法通過這部分。
這是一組數據,每行一個數據項。 您必須對多組數據進行調整。
int i = 0;
while (readInput.hasNext())
{
if (i == 0)
{
firstName = readInput.nextLine();
}
else if (i == 1)
{
lastName = readInput.nextLine();
}
else if (i == 2)
{
houseNumber = readInput.nextLine();
}
else if (i == 3)
{
street = readInput.nextLine();
}
else if (i == 4)
{
city = readInput.nextLineLine();
}
else if (i == 5)
{
state = readInput.nextLine();
}
else if (i == 6)
{
zip = readInput.nextLine();
}
else if (i == 7)
{
productDescription = readInput.nextLine();
}
else if (i == 8)
{
productPrice = readInput.nextDouble();
}
i += 1;
} // End while
老實說,您的程序在很多方面都存在問題。 但是,以下是解決您提到的不匹配問題的說明。
readInput.nextLine()
將讀取當前行的其余部分 。 因此,在閱讀“百老匯”之后,掃描程序會停留在同一行,當您調用nextLine時,掃描程序會產生“百老匯”行中剩余的任何內容,這是一個空字符串。
為了避免這種情況,請執行
street = readInput.next();
readInput.nextLine();
刪除當前行(例如“ Broadway”)。 然后打電話
city = readInput.nextLine();
這樣,程序將按您的預期顯示“ New York”。 正如湯姆在評論中提到的那樣,有關更多詳細信息,請看這里的問題。
除了掃描儀問題外,您的程序在結束位置上也模棱兩可–您未提供右括號。 考慮到您的輸入已損壞,雖然while循環似乎是多余的:“ 199.99”行之后它不再與您的代碼匹配。 請在此處輸入完整的代碼,然后修改示例輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.