Search code examples
javanumberformatexception

NumberFormatException when parsing an integer


I have a file that contains lots of movie titles along with their years, rating out of 5 stars, and length. I'm attempting to read the file and store the titles, year, rating, and length as variables. The problem I've encountered is in my code for retrieving the year. The code compiles, but then upon running throws a NumberFormatException when it gets to a movie with multiple years (for example, it is listed as 2006-2009). Here is my code.

while((line = bufferedReader.readLine()) != null) {
                //System.out.println(line);
                for(int i = 0; i < line.length(); i++)
                {

                    if(line.charAt(i) == ')' || line.charAt(i) == '-')//get year
                    {
                        yr = line.substring(yearStart,i);
                        year = Integer.parseInt(yr);
                    }

                }
                System.out.println(year);

            }

Shouldn't line.charAt(i) == '-' in my if statement take care of this issue?

EDIT: The below code is where yearStart comes from.

if(line.charAt(i) == '(')//get title
                    {
                        title = line.substring(0,i);
                        yearStart = i+1;
                    }

The file is formatted like this: title (year) | rating, length Sorry, I should have included that originally.

EDIT #2: Here's a sample of part of the file, if that helps

!Women Art Revolution (2010) |   3 stars, 1hr 22m
#1 Cheerleader Camp (2010) |   3 stars, 1hr 35m
$5 a Day (2008) |   3.4 stars, 1hr 37m
'night, Mother (1986) |   3.7 stars, 1hr 36m
'Til Death (2006-2009) |   3.7 stars, 4 Seasons//This is the one that causes the error
@Suicide Room (2011) |   3.4 stars, 1hr 51m
... And God Spoke (1993) |   2.8 stars, 1hr 22m

Solution

  • What happens once you have found the year? The loop will still run until the end of the line, trying to parse things as numbers every time it finds a ) or a -.

    Maybe you should exit the loop once you have found the year.

                    if(line.charAt(i) == ')' || line.charAt(i) == '-')//get year
                    {
                        yr = line.substring(yearStart,i);
                        year = Integer.parseInt(yr);
                        break; // year found, no point in looping more
                    }
    

    Or maybe you should reset the yearStart index so that the second year can be parsed.

                    if (line.charAt(i) == '-')
                    {
                        // parse first year
                        yr = line.substring(yearStart,i);
                        year = Integer.parseInt(yr);
                        yearStart = i + 1; // reset to parse second year
                    }
                    else if (line.charAt(i) == ')')
                    {
                        yr = line.substring(yearStart,i);
                        year = Integer.parseInt(yr);
                        break; // year found
                    }