1

I have a class defined like this, with the appropriate getter and setter methods...

public class Album {
    private int id;
    private String artist;
    private String name;
    private int published;
}

I also have a .csv file that stores this content for a number of Albums. In the file, one line represents one Album.

I'm trying to read the information from the .csv file, and then use the setters of the Album class to assign the values. Here is my code...

public Map<Integer, Album> load() {

    Scanner scanner = new Scanner(fileName);
    Map<Integer, Album> loadedAlbums = new HashMap<Integer, Album>();

    while(scanner.hasNextLine()) {
        Album album = new Album();

        String[] albumDivided = scanner.nextLine().split(","); 
        //in the .csv file every unit of information is divided by a comma.

        album.setId(Integer.parseInt(albumDivided[0])); //this is line 11.
        album.setArtist(albumDivided[1]);
        album.setName(albumDivided[2]);
        album.setPublished(Integer.parseInt(albumDivided[3]));

        loadedAlbums.put(album.getId(), album);

    }

    return loadedAlbums;
}   

However, trying to use this code, I get the following Exception:

java.lang.NumberFormatException: For input string: "albums.csv" at line 11.

Could you please help me to understand the cause of this problem.

1
  • Oh and the format of information in the file is: "1.,Artist Name,Album name,Published" Commented Apr 11, 2012 at 13:26

3 Answers 3

1

Well the problem is described to you by the Exception...

A NumberFormatException would have been triggered by one of your Integer.parseInt() lines. The line of your code that is triggering the exception is Line 11 (as per the exception message) - not sure which one this is but its probably the first Integer.parseInt() line.

Your code is trying to convert the value "albums.csv" to a number, which is obviously isn't. So somewhere in your CSV file you must have a line that contains the value albums.csv where it is expecting a number.

Hope this helps pinpoint the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I realized the problem now.
1

Since you don't want the whole solution here is a hint to resolve your problem:

You should take a look at the API documentation of the Scanner class. Take a really close look on the constructor that expects a single String parameter (as you use it in your code).

1 Comment

Thanks for the help. Gotta study the API more carefully in the future
1

As far as I can tell, albumDivided[0] will containt "1." which will not be able to parse to an integer because of the dot. Either remove the dot from your csv file, or create a new string that removes the dot before you parse it to Integer. The approach might look something like this:

String newString;
for(int i=0;i<albumDivided[0].length-1;i++){ //length -1 to remove the dot
   newString = newString + albumDivided[0].charAt(i); //get the string stored in albumDivided[0] and add each char to the new string
}

1 Comment

Thanks, I will keep this in mind if I encounter problems with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.