Alex_E
Alex_E

Reputation: 39

NumberFormatException pointing to a double input on an integer

I am currently running a ticket issuing program which I believed was complete, however after I have created a few ticket bookings and written them to a text file, I try to run after a second or third input and it gives me this exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "59.75"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at TicketBoxProject.TicketBoxMain.main(TicketBoxMain.java:94)
try {
        br = new BufferedReader(new FileReader("BookingsList.txt"));
        FileReader fr = new FileReader("BookingsList.txt");
        br = new BufferedReader(fr);

        String str;
        while ((str = br.readLine()) != null) {
            Bookings booking = new Bookings(); // single job of class
                                                // JobList
            booking.setFName(str);
            str = br.readLine();
            booking.setSName(str);
            str = br.readLine();
            booking.setHouseNo(str);
            str = br.readLine();
            booking.setStreet(str);
            str = br.readLine();
            booking.setTown(str);
            str = br.readLine();
            booking.setPostCode(str);
            str = br.readLine();
            booking.setEmail(str);
            str = br.readLine();
            booking.setCardType(str);
            str = br.readLine();
            booking.setCardNumber(Long.parseLong(str));
            str = br.readLine();
            booking.setCardExpiryDate(str);
            str = br.readLine();
            booking.setCardSecurityNo(Integer.parseInt(str));
            str = br.readLine();
            booking.setTicketsReqd(Integer.parseInt(str));
            str = br.readLine();
            booking.setTotal(Double.parseDouble(str));
            str = br.readLine();
            booking.setRefNo(Integer.parseInt(str));
            str = br.readLine();
            booking.setDate(str);
            str = br.readLine();
            booking.setArtist(str);
            str = br.readLine();
            booking.setVenue(str);
            str = br.readLine();
            booking.setCity(str);
            str = br.readLine();

            bookings.add(booking); // add to arraylist
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    br.close();

java:94 points to the setTicketsReqd line, however the "59.75" should refer to the setTotal line below this, as you can see from the code, I have ensured the TicketsReqd is an integer and the Total is a Double. If anyone knows a quick fix apart from deleting the file from the textfile I would appreciate it as I can't understand why the error message is telling me there is a fault from the input "59.75"(which is the total from a previous written-to-textfile booking) and then points to a different line of code.

Upvotes: 0

Views: 159

Answers (1)

rgettman
rgettman

Reputation: 178343

In your code, I count 19 calls to the readLine() method, both as part of the while loop condition and the body of the loop. But I see only 18 setXyz methods called on the Bookings instance.

The last readLine() call in the loop is ignored; it's replaced by the call to readLine() in the condition of the while loop. It will read in the first line in the next entry and ignore it. Then the first readLine call will read in what was meant to be the second line in the booking. This likely is causing an "off by one" error, so that 59.75 is now being read in by the line that calls Integer.parseInt.

Remove that last readLine() call.

Upvotes: 1

Related Questions