reset password
Author Message
dchou4
Posts: 4
Posted 12:31 Sep 29, 2017 |

I used pretty much the same method from the lecture 7 (for parsing a file), but when I run the method below I get the following error(any suggestions would be great): 

java.lang.NumberFormatException: For input string: ""

 

I attached my .csv file

 

    public void readFile() {

        try {

            System.out.println("Enter a filepath to read from "); 

            Scanner in = new Scanner(System.in);

            File inFile = new File(in.next()); // File to read from. 

            Scanner freader = new Scanner(inFile);

            

            String line; 

            String[] fields; 

            int iD;

            String date;

            String monster;

            String location;

            String witness;

            while (freader.hasNextLine()) { 

                line = freader.nextLine(); 

                System.out.println(line);

                fields = line.split(",");        

                iD = Integer.parseInt(fields[0]);

                date = fields[1];

                monster = fields[2];

                location = fields[3];

                witness = fields[4];

                attacks.add(new MonsterAttack(iD,date,monster,location,witness));        

            }

            freader.close();

            in.close();

        }catch(IOException e) {

            System.err.println(e);

            System.exit(1);

        }

    }

Attachments:
V_agu30
Posts: 8
Posted 16:09 Sep 30, 2017 |

Do you know whether we read the information from a separate .csv file and add it to an ArrayList or we use the user's input to fill out the .csv we are supposed to create.  

dchou4
Posts: 4
Posted 16:37 Sep 30, 2017 |

It shouldn't really matter. You should be able to create a new list from your program. Then delete and bring it back from the csv. file you created.