0

I am receiving a InputMismatchException

java ShoppingTime Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at ShoppingTime.main(ShoppingTime.java:88)

From my understanding it is because I am not providing enough inputs in my main function? But I am lost on what I need to add. would it be another price input? I don't think so bc I am only taking a string input and integer input.

public class ShoppingTime{
public static void main(String args[]) throws FileNotFoundException {
    File read = new File("Products.txt");
    Scanner sc = new Scanner(read);
    ArrayList<Product> ar = new ArrayList<Product>();
    while (sc.hasNextLine()) {
        String product = sc.nextLine();
        int price = sc.nextInt();
        ar.add(new Product(product, price));
    }
    int ch;
    Cart c = new Cart();
    sc = new Scanner(System.in);
    do {
        System.out.println("Menu");
        System.out.println("1 Add item");
        System.out.println("2 View Cart");
        System.out.println("3 Clear Cart");
        System.out.println("4 Checkout");
        System.out.println("5 Exit");
        ch = sc.nextInt();
        if (ch == 1) {
            c.addItem(ar);
        }
        if (ch == 2) {
            c.viewCart();
        }
        if (ch == 3) {
            c.clearCart();
        }
        if (ch == 4) {
            c.checkOut();
        }

    } while (ch != 5);

}

}

Willy
  • 57
  • 7
  • 1
    Your loop to read in data looks kind of off: First you do `String product = sc.nextLine();` and then you do `int price = sc.nextInt();` after that. So you first say "read in the whole line" followed by "Now read a number". But if you already told scanner to read the while line where is the number supposed to come from? You didn't think it important to show us what exactly you are trying to read/parse but somehow I would suspect that the number you are trying to get is probably part of the line. – OH GOD SPIDERS Feb 23 '21 at 17:34
  • It definitely is thank you for that. the .txt file looks like so Pills 39.99 – Willy Feb 23 '21 at 17:46
  • So the 2 values are separated by a space? In that case using `next()` instead of `nextLine()` could work for only getting the `"Pills"` part. But you have the added Problem that `39.99` is not an integer (whole number) and therefor you can not use an `int` and the `nextInt()` method for that number. You'll need to use a number type that supports fractions like `double` for example – OH GOD SPIDERS Feb 23 '21 at 17:50
  • for the price I should just use nextDouble(). I was doing that but still getting an error. I assume because I used nextLine() instead of next(). – Willy Feb 23 '21 at 18:07
  • That worked so far thank you very much! – Willy Feb 23 '21 at 18:08

1 Answers1

0

all the line is a string, so you have read the whole line including the wrapped int in the first nextLine() call.you should just read with nextLine() and parse the int out of it.

  • Awesome thanks that is what I am trying to do now as I have to read the file as a string, and parse the int and store that to have a running total. – Willy Feb 25 '21 at 19:36