我一直在努力与扫描仪类。我似乎无法绕过它的方法说唱我的头。我试图运行一套看起来适合我的代码。我已经尝试了一些调整,但仍然没有。为什么我“在线程异常‘收到此错误扫描程序类和线程“主”中的异常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