0

I am fairly new to Java and have come across an issue which I know should be fairly easy to resolve. However, I cannot figure out where I am going wrong and how I can go about solving this issue. As you can see from my code snippet below I am trying reading from a file using the classical FileReader and I have used a while loop to read the entire file. However, this works all well and good but I would like to multiply the read.nextint() with read.nextdouble() However when I try and multiply them where my Income is it throws an error message. Any solution would be great! Thanks.

    FileReader file = new FileReader(fileName);
    Scanner read = new Scanner(file);
        while (read.hasNext()) {
            System.out.print("Room Type: " + read.next());
            System.out.print(", Bookings: " + read.nextInt());
            System.out.print(", Room Price: " + read.nextDouble());
            System.out.println(", Income: " + read.nextDouble() * Double.valueOf(read.nextInt()));
            System.out.println(", Tax: " + TaxRate + "\n\n");
        }

This is the error message:

Room Type: Single, Bookings: 5, Room Price: 23.5Exception 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.nextDouble(Scanner.java:2564)
at RoomTaxSystem.Room_Tax_System.main(Room_Tax_System.java:24)

This is the data I am trying to read from the file

Single

5

23.50

Double

3

27.50

Suite

2

50.00
blackbrandt
  • 2,010
  • 1
  • 15
  • 32
  • Check this out and see if it can help you! https://stackoverflow.com/questions/14027537/why-am-i-getting-inputmismatchexception – lorenzozane Nov 11 '20 at 15:17
  • First, I advise you to read the data into a data structure: Maybe an ArrayList of Booking objects. Once you have read the data, you do the processing. Easier to debug and less stress :-) – Tarik Nov 11 '20 at 15:18
  • Can you add an example of the data file? It may be a problem in the file. – John Mercier Nov 11 '20 at 15:23
  • I've just added the data from my file to show you what I am trying to read. :) – Joseph Michael Nov 11 '20 at 15:27
  • 1
    You cannot read the values twice. You must save them in a variable and then you can use them to calculate your values – Thallius Nov 11 '20 at 15:31

2 Answers2

2
FileReader file = new FileReader(fileName);
Scanner read = new Scanner(file);
    while (read.hasNext()) {
        System.out.print("Room Type: " + read.next());
        int bookings = read.nextInt();
        System.out.print(", Bookings: " + bookings);
        double price = read.nextDouble();
        System.out.print(", Room Price: " + price);
        System.out.println(", Income: " + bookings * price);
        System.out.println(", Tax: " + TaxRate + "\n\n");
    }
Thallius
  • 2,482
  • 2
  • 18
  • 36
  • Thanks for your response! This worked. One further query, is there a way in which I could calculate a total at the end based on the number of variables I have for income? So for example If I wanted to see what the income was in total could I do this? – Joseph Michael Nov 11 '20 at 16:46
  • Sure, declare a variable double total = 0 outside of the while loop and inside of the loop add the income to the total. Behind the loop you can then print the total amount – Thallius Nov 11 '20 at 16:47
  • Brilliant worked perfectly! Thanks for your help :) – Joseph Michael Nov 11 '20 at 16:53
0

The InputMismatchException is thrown because your second read.nextDouble() is trying to interpret the "Double" in your file as a Double Type. If you want to use the first read.nextDouble() value then you have to save it in a variable as Claus said in his answer.

Mizo10
  • 45
  • 7