user2035796
user2035796

Reputation: 67

Number format exceptions when reading a file in Java

Hey all so I'm trying to read the values from a text file so I can use it for something else- However, I keep getting a 'lava.lang.NumberFormatException' when I try to run. This is a snippet of my code and where the problem seems to lie.

   public void readData(String fileName) {

    try {

        Scanner in = new Scanner(new File(fileName));

        String[] line;
        int mid;   //user-id
        int uid; //isbn
        int rating;  //book rating
        //int start =0;
        //String date;
        //System.out.println(in.nextLine());
        in.nextLine();
        while(in.hasNextLine()) {
            //if(start != 0 ){
                line = in.nextLine().split(";");
                System.out.println(line[0].trim().replace("\ufeff", "") + " length: "+  line[0].length());
                //System.out.println(line[0]+ " -----------++++++++++++++");
                mid = Integer.parseInt(line[0].trim());
                uid = Integer.parseInt(line[1].trim());  //isbn change
                rating = Integer.parseInt(line[2].trim());

                addToMovies(mid, uid, rating);
                addToCust(mid, uid, rating);
            //} //else {
                //start ++;
            }

        //}
    }
    catch(FileNotFoundException e) {
        System.out.println("Can't find file " + fileName);
        e.printStackTrace();

    }
    catch(IOException e) {
        System.out.println("IO error");
        e.printStackTrace();
    }

And this is the data I'll be reading:

      "User-ID;""ISBN"";""Book-Rating"""
    "276725;""034545104X"";""0"""
    "276726;""0155061224"";""5"""
    "276727;""0446520802"";""0"""
    "276729;""052165615X"";""3"""
    "276729;""0521795028"";""6"""
    "276733;""2080674722"";""0"""
    "276736;""3257224281"";""8"""
    "276737;""0600570967"";""6"""
    "276744;""038550120X"";""7"""

When I run it, I get the following error:

    "276725 length: 7
usage: java MemReader sourceFile destFile
java.lang.NumberFormatException: For input string: ""276725"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at memreader.MemReader.readData(MemReader.java:76)
at memreader.MemReader.main(MemReader.java:265)

It would appear that maybe java is reading invisible characters because I have tested it by printing the length of the first index and the number of characters is more than there should be. Any help will be much appreciated.

Upvotes: 0

Views: 196

Answers (2)

Shrinivas Shukla
Shrinivas Shukla

Reputation: 4463

The value of line[0] now is "276725.

Try this.

mid = Integer.parseInt(line[0].substring(1).trim());

It will remove the leading " and the remaining value will be numeric.

Upvotes: 0

cahen
cahen

Reputation: 16676

Quotes are not ignored by the parser, remove them first.

Change

line = in.nextLine().split(";");

to

line = in.nextLine().replaceAll("\"", "").split(";");

Upvotes: 2

Related Questions