-1

Getting the following error:

101
Exception in thread "main" java.lang.NumberFormatException: For input string: "false"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

For the following code.

public static void main(String[] args) throws FileNotFoundException {
    Scanner file = new Scanner(new FileReader("rooms.txt"));

    while(file.hasNext()) {

        int no = Integer.parseInt(file.nextLine());
        System.out.println(no);
        String type = file.nextLine();
        double price = file.nextDouble();
        boolean balcony = Boolean.parseBoolean(file.nextLine());
        boolean lounge = Boolean.parseBoolean(file.nextLine());
    }
}

}

The file that its reading from is:

    101
    Single
    23.50
    false
    false

i don't understand whats going wrong. Its loading the "101" from the file then it gets an error. Also i have read through all the similar questions and cant find a solution so don't mark this as a duplicate please.

Peter yo
  • 1
  • 1
  • 2
    Iteration 1: `file.nextLine()`: "101" -> 101, `file.nextLine()`: "Single", `file.nextDouble()`: 23.5, `file.nextLine()`: "" -> false, `file.nextLine()`: "false" -> false. --- Iteration 2: `file.nextLine()`: "false" -> **error** – Andreas Dec 09 '16 at 05:26

1 Answers1

2

Follow the pattern of

 int no = Integer.parseInt(file.nextLine());

by changing

double price = file.nextDouble();

to

double price = Double.parseDouble (file.nextLine());

If you just read the nextDouble then the rest of the line still remains `\n' and the next statement will then consume it, but processing will then be out-of-sync with your code

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62