1
votes

I have been struggling with the scanner class. I just can't seem to rap my head around it's methods. I'm trying to run set of code which seems right to me. I've tried a few tweaks but still nothing. Any hints of why i am receiving this error

"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.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at 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();

}

2

2 Answers

1
votes

There is number of issue at your code. Never called nextLine() after while(hasNext()). Your while loop should be

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

You called nextTerm.next() at every if-else statement. You should assign a String variable named operation.

   String operation=nextTerm.next();

   if (operation.equalsIgnoreCase("insert")) {
      ....
   } else if (operation.equalsIgnoreCase("delete")) {
    ..........
   } 
   ...
   else if (operation.equalsIgnoreCase("product")) {
     .....
   } 
1
votes

InputMismatchException is Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

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

try change above to as below.(If you have first 2 integer inputs in 2 separate lines, otherwise try parsing them after read them using fileData.nextLine().split(" ") )

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

if 2 integers in the same line

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

good if you can post your Polynomial class also...