Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with File Processing

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with File Processing

    my code is compiling but it won't run. can someone help me with this? i've been having trouble for days. my instructor said that
    Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
    the nextLine() is not compatible with the parseInt but i have no idea what to change on that line. any suggestions?

     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
        		fr = new Scanner (new File("students.csv"));
     
        	// 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();
    			System.out.println(s2);
    		} catch (Exception i) {
    			i.printStackTrace();
    		}
     
     
    		// close input file...
    		ois.close();
     
    		// verify read data...
    		//ystem.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();
        	*/
        }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with File Processing

    What does the first line of the data file look like?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with File Processing

    like this
    2123259,AVILA,JEREMY RAYMUND,T,BSIT,1

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with File Processing

    So this part:
    Integer.parseInt(fr.nextLine())
    of the statement:
    Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
    is attempting to convert the whole first line of the file, which is this:
    2123259,AVILA,JEREMY RAYMUND,T,BSIT,1
    to an integer, and it's complaining (as it should).

    Review your code comment:
    // Read the first line of the text file as basis for the number
    // of elements for the array of Students to be declared
    Is the first line of the data file that you posted going to tell you how many Students to create? No. Is that first line missing, or is the file right and the code comment wrong?

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with File Processing

    Okay, so I've changed the line
    Student[] studs = new Student[Integer.parseInt(fr.nextLine())];
    to a more straight forward revision just to see what happens like ... = new Student[107]; and now I'm getting this kind of results

    Student ID: 2115232
    Student name: VILLANUEVA, SHEENA M.
    CourseBSIT-0
    null
    null
    null
    null
    null
    java.lang.ClassCastException: [LStudent; cannot be cast to Student
    at SampleFileProcessing.printToFile(SampleFileProcess ing.java:72)
    at SampleFileProcessing.run(SampleFileProcessing.java :47)
    at SampleFileProcessing.main(SampleFileProcessing.jav a:10)
    but I don't know why..

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with File Processing

    The error refers to a cast of LStudent to a Student, as in:

    LStudent lStudent = new LStudent();
    Student student = (Student)lStudent;

    What's LStudent? Did you rename the Student class as part of your versioning scheme and forget to change a statement like this one:

    Student s2 = (Student) ois.readObject();

Similar Threads

  1. [SOLVED] JAVA Text File Processing with REGEX
    By tonionio in forum Java Theory & Questions
    Replies: 9
    Last Post: February 28th, 2013, 05:01 AM
  2. java file processing beginner level
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2012, 09:54 PM
  3. Processing
    By KevinWorkman in forum Other Programming Languages
    Replies: 4
    Last Post: September 6th, 2011, 10:30 AM
  4. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 03:27 PM
  5. ERROR WHILE PROCESSING JSF FILE
    By Pankaj in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 10th, 2010, 12:42 AM

Tags for this Thread