hi guys, i'm really having a trouble with my conversion code... it says the NumberFormatException with this "Student[] studs = new Student[Integer.parseInt(fr.nextLine())];" but i have no idea why. can someone help me?

import java.util.Scanner;
import java.io.*;

public class SampleFileProcessing implements Serializable{
    private static Scanner fr;

    public static void main(String[] args) throws IOException {
        SampleFileProcessing tfr = new SampleFileProcessing();

        tfr.run();
    }

    private static void run() throws IOException {
        // To read the input file
        try {
            fr = new Scanner (new File("students.csv"));
        } catch (Exception e) {
            e.printStackTrace();
        }



        // Read the first line of the text file as basis for the number
        // of elements for the array of Students to be declared
        Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
        int index = 0;

        // Read each of the records contained in the text file
        // and store them into the elements of the array
        while (fr.hasNextLine()){
            String[] temp = fr.nextLine().split(",");

            // Store the values to the following variables
            String id = temp[0];
            String lastName = temp[1];
            String firstName = temp[2];
            String middleInit = temp[3];
            String course = temp[4];
            int year = Integer.parseInt(temp[5]);

            // Use the values in instantiating Student object
            studs[index] = new Student(id, lastName, firstName, middleInit, course, year);
            // Alternatively, the previous lines can be shortened
            // using the statement below:
            // studs[index] = new Student(temp[0], temp[1], Integer.parseInt(temp[2]),
            //                           Integer.parseInt(temp[3]));


        }

        // output
        printToScreen(studs);
        printToFile(studs);
    }

    private static void printToScreen(Student[] st) {
        // To output the array objects (screen) using the enhanced for statement
        for (Student s: st) {
            System.out.println(s); // toString() method is called automatically
        }
    }

    private static void printToFile(Student[] s) throws IOException {
        // To output records in an output file: output.txt
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.dat"));

        //write object to output file
        oos.writeObject(s);

        //close file
        oos.close();

        // open created file as input file...
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.dat"));

        // read object data from file into object variable (note the type cast)...
        try{
            Student s2 = (Student) ois.readObject();
        } catch (Exception i) {
            i.printStackTrace();
        }


        // close input file...
        ois.close();

        // verify read data...
        System.out.println(s2);

        //PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"));

        /*for (int i = 0; i < s.length; i++){
            outFile.println("ID: " + s[i].getID());
            outFile.println("Name: " + s[i].getLastName() + ", " + s[i].getFirstName() + " " + s[i].getMiddleInit() + ".");;
            outFile.println("Course and Year: " + s[i].getCourse());
            outFile.println("================================");
        }
        outFile.close();
        */
    }

}

I hate to disagree with masijade, but the API doc for Scanner says

public String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.

It may be worth printing the result of nextLine() at that point before trying to parse it so you can see exactly what's happening

That could very well be (and IS considering the API doc quote), I just guessed this time. I hate Scanner and DO NOT use it. Bleah!

;-)

I also avoid Scanner - I know it's intended as a simple system for beginners, but I've lost count of the number of posts we've had here where someone has tried nextInt() ... nextLine() and got baffled.

mrosario: You are doing exactly what I would do, ie reading whole lines then splitting/parsing them yourself so you have complete control. For that you don't need a Scanner at all, just a BufferedReader and its readLine() method

thanks for the suggestions. i will try them especially the bufferedreader

This article has been dead for over six months. Start a new discussion instead.