1

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 Answers 2

1

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")) {
     .....
   } 
3
  • Is hasNextLine() a proper scanner method? I haven't tried the code yet i am just asking because we were taught hasNext() returns true if there is another non whitespace string and false if there isn't. Are they the samething?
    – Jensenwd
    Dec 9, 2013 at 3:25
  • @Brwski, hasNext() traverses space by space, whereas hasNextLine() with line by line. As you used nextLine() after while loop, so you should use hasNextLine().
    – Masudul
    Dec 9, 2013 at 3:28
  • It didn't work. Maybe it has something to do with my coeff and expo variables. When i try to echoprint using those variables, would that be a problem? Is there another way to echo print what was added or deleted without using those variables?
    – Jensenwd
    Dec 9, 2013 at 3:59
1

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.