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.
JavaScript
x
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 + "nn");
}
This is the error message:
JavaScript
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
JavaScript
Single
5
23.50
Double
3
27.50
Suite
2
50.00
Advertisement
Answer
JavaScript
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 + "nn");
}