2013-12-09 61 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类也......

相关问题