我一直在努力與掃描儀類。我似乎無法繞過它的方法說唱我的頭。我試圖運行一套看起來適合我的代碼。我已經嘗試了一些調整,但仍然沒有。爲什麼我「在線程異常‘收到此錯誤掃描程序類和線程「主」中的異常java.util.InputMismatchException錯誤
的任何暗示主’java.util.InputMismatchException處 java.util.Scanner.next java.util.Scanner.throwFor(來源不明)(來源不明)處 java.util.Scanner.nextInt(來源不明) java.util.Scanner.nextInt(來源不明)在 PolynomialTest.main(PolynomialTest.java:18)」
public class PolynomialTest {
public static void main(String[] args) throws IOException {
Scanner fileData = new Scanner(new File("operations.txt"));
Polynomial math = new Polynomial();
int coeff = fileData.nextInt();
int expo = fileData.nextInt();
while (fileData.hasNext()) {
Scanner nextTerm = new Scanner(fileData.nextLine());
if (nextTerm.next().equalsIgnoreCase("insert")) {
math.insert(coeff, expo);
System.out.println("The term (" + coeff + ", " + expo
+ ") has been added to the list in its proper place");
System.out.println("The list is now: " + math.toString());
} else if (nextTerm.next().equalsIgnoreCase("delete")) {
math.delete(coeff, expo);
System.out.println("The following term has been removed ("
+ coeff + ", " + expo + ")");
System.out.println("The list is now: " + math.toString());
} else if (nextTerm.next().equalsIgnoreCase("reverse")) {
math.reverse();
System.out
.println("The list was reversed in order and the list is now: "
+ math.toString());
} else if (nextTerm.next().equalsIgnoreCase("product")) {
System.out.println("The product of the polynomial is: "
+ math.product());
} else {
System.out.println("Not a recognized input method");
}
nextTerm.close();
}
PrintWriter save = new PrintWriter("operations.txt");
save.close();
fileData.close();
}
hasNextLine()是否正確的掃描器方法?我沒有試過代碼,但我只是問,因爲我們被教導hasNext()返回true,如果有另一個非空白字符串,如果沒有,則返回false。它們是一樣的嗎? – Jensenwd
@Brwski,hasNext()遍歷空間,而hasNextLine()逐行。正如你在while循環之後使用nextLine(),所以你應該使用hasNextLine()。 – Masudul
它沒有工作。也許這與我的coeff和expo變量有關。當我嘗試使用這些變量進行echoprint時,會是一個問題嗎?是否有另一種方式來回顯打印添加或刪除的內容而不使用這些變量? – Jensenwd