-1

I have 20k lines of .txt file. My problem is I got this error and message. I created four classes. I extend the Process into Converter. Then I want to convert the name, price, quantity, and total into Objects from my records.txt.

Exception in thread "main" java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:943)
        at java.base/java.util.Scanner.next(Scanner.java:1598)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2263)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2217)
        at InvoiceConverter.ConvertStringToObject(InvoiceConverter.java:18)
        at MyMainClass.main(MyMainClass.java:7)

Here's the example of my .txt file

Banana
125.50
3
376.50

Here's my Converter.class

public class Converter extends Process {
    Converter(String name, double price, int quantity, double total) {
        super(name, price, quantity, total);
    }
    public Converter() {

    }
    public void ConvertStringToObject() {
        String fileName = "records.txt";
        List<Process> invoice = new ArrayList<>();
        try (Scanner sc = new Scanner(new File("records.txt"))){
            int count = sc.nextInt();
            for (int i = 0; i < count; i++) {
                String name = sc.nextLine();
                double price = sc.nextDouble();
                int quantity = sc.nextInt();
                double total = sc.nextDouble();
                invoice.add(new Process(name, price, quantity,total));
            }
        } catch (IOException e) {
            System.out.println("Error Occurred");
            e.printStackTrace();
        }
    }
}

Here's my Process.class

public class Process {
    String name;
    double price;
    int quantity;
    double total;

    Process(String name, double price, int quantity, double total) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.total = total;
    }
    public Process() {

    }
}

Here's my records.txt enter image description here

How can I solve this problem?

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
Kilala Ka
  • 13
  • 3
  • 2
    You need to realize that linebreaks matter here. `String name = sc.nextLine();` reads a whole line, including the linebreak. the following `sc.nextDouble();` consumes the double such as the `125.50`, but the next char in the input is still a linebreak. That's why the subsequent `sc.nextInt();` throws an exception. Consume the linebreak using `nextLine()`. – f1sh Apr 13 '22 at 12:52
  • 2
    Does your file start with `Banana`, or does it start with an integer? – Andy Turner Apr 13 '22 at 12:58
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – QBrute Apr 13 '22 at 13:00
  • @AndyTurner it starts with Barbecue Ribs I use faker to generate dummy records – Kilala Ka Apr 13 '22 at 13:07
  • 1
    @KilalaKa but your code expects a number in the first line. Did you not write that code? – f1sh Apr 13 '22 at 13:15
  • @f1sh you can see the image above. My records.txt start with string which is the Barbecue Ribs. I edit the codes. I try it if gonna work. – Kilala Ka Apr 13 '22 at 13:28

1 Answers1

0

You need to consume linebreaks after reading numbers. You need to call sc.nextLine() after reading a number.

As Andy mentioned in the comments, the file is supposed to have the number of Process objects in the first line. So your file needs to start with a single line containing a number, and you also need to consume the linebreak after that.

You can consume the file like this:

int count = Integer.parseInt(sc.nextLine()
for (int i = 0; i < count; i++) {
  String name = sc.nextLine();
  double price = Double.parseDouble(sc.nextLine());
  int quantity = Integer.parseInt(sc.nextLine());
  double total = Double.parseDouble(sc.nextLine());
  invoice.add(new Process(name, price, quantity,total));
}
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • Exception in thread "main" java.lang.NumberFormatException Create breakpoint: For input sting: "Barbeque Ribs" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:668) at java.base/java.lang.Integer.parseInt(Integer.java:784) at InvoiceConverter.ConvertStringToObject(InvoiceConverter.java:18) at MyMainClass.main(MyMainClass.java:7) – Kilala Ka Apr 13 '22 at 13:11
  • the first line in records.txt is Barbeque Ribs – Kilala Ka Apr 13 '22 at 13:37
  • @KilalaKa but why? Your code needs to be compatible to your code. It's not. Agree on something. – f1sh Apr 13 '22 at 13:38