2013-12-09 94 views
1

我一直在努力與掃描儀類。我似乎無法繞過它的方法說唱我的頭。我試圖運行一套看起來適合我的代碼。我已經嘗試了一些調整,但仍然沒有。爲什麼我「在線程異常‘收到此錯誤掃描程序類和線程「主」中的異常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(); 

}

回答

1

您的代碼有多少問題。 while(hasNext())之後從未呼籲nextLine()。你while迴路應

while (fileData.hasNextLine()) { 
    Scanner nextTerm = new Scanner(fileData.nextLine()); 

你在每一個if-else說法叫nextTerm.next()。您應該指定一個String變量命名操作。

String operation=nextTerm.next(); 

    if (operation.equalsIgnoreCase("insert")) { 
     .... 
    } else if (operation.equalsIgnoreCase("delete")) { 
    .......... 
    } 
    ... 
    else if (operation.equalsIgnoreCase("product")) { 
    ..... 
    } 
+0

hasNextLine()是否正確的掃描器方法?我沒有試過代碼,但我只是問,因爲我們被教導hasNext()返回true,如果有另一個非空白字符串,如果沒有,則返回false。它們是一樣的嗎? – Jensenwd

+0

@Brwski,hasNext()遍歷空間,而hasNextLine()逐行。正如你在while循環之後使用nextLine(),所以你應該使用hasNextLine()。 – Masudul

+0

它沒有工作。也許這與我的coeff和expo變量有關。當我嘗試使用這些變量進行echoprint時,會是一個問題嗎?是否有另一種方式來回顯打印添加或刪除的內容而不使用這些變量? – Jensenwd

1

InputMismatchException時由掃描器拋出,表明檢索到的令牌不匹配的期望類型的模式,或者該標記超出範圍的預期的類型。

int coeff = fileData.nextInt(); 
int expo = fileData.nextInt(); 

嘗試改變以上如下所示。(如果在2個單獨的行第一2個整數輸入,否則嘗試後解析他們使用fileData.nextLine().split(" ")讀取它們)在同一

int coeff = Integer.parseInt(fileData.nextLine()); 
int expo = Integer.parseInt(fileData.nextLine()); 

如果2個整數線

String s[] = fileData.nextLine().split(" "); 
    int coeff = Integer.parseInt(s[0]); 
    int expo = Integer.parseInt(s[1]); 

好,如果你可以發表你的Polynomial類也......

相關問題