7 Replies - 3596 Views - Last Post: 06 April 2011 - 06:30 PM Rate Topic: -----

#1 ytf1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-April 11

reading from file and store in Vector in java

Posted 06 April 2011 - 03:51 AM

this is about read from a file and stored in the vector and then printout. when I run the code,I got error messages as:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at CollegeStudent.readFromFile(CollegeStudent.java:75)
at Driver.main(Driver.java:35)
another question is how to print out the content of the vector based on my codes, I could not figure it out. any suggestions will be appreciated.
import java.io.*;
import java.lang.String;
import java.util.Vector;
public class CollegeStudent extends Student{
	private String degree; 
	private String major;
	
	public CollegeStudent(){
		degree = null;
		major = null;
	}
	//
	
	public CollegeStudent(String dg, String mj){
		degree = dg;
		major = mj;
	}
	
	public String getDegree(){
		return degree;
	}
    //
	public String getMajor(){
		return major;
	}
	//
	public void setDegree(String dg){
		degree = dg;
	}
	
	//
	public void setMajor(String mj){
		major = mj;
	}
	//
	public void read(){
		
		 super.read();
			System.out.print("Enter the major: ");
		    major = input.nextLine();
		    major = major.toLowerCase();
	}
	//
	public void setDegree(){
		
		 String a = "cs engineering math biology chemistry physics";
		 int index = a.indexOf(major);
		 if (index != -1){
			 degree = "BS";
		 }
		 else {
			 degree = "BA";
		 }
	}
	//
	public void display(){
		
		super.dispaly();
		System.out.println("Major: " + major);
		System.out.println("Expected Degree: " + degree);
		System.out.println("");
	}
	public void readFromFile(BufferedReader inputFile) throws IOException {
		String str = "";
	   if (inputFile != null){	
			str = inputFile.readLine();
			if(str != null) {
				setName(str);
				
			  	str = inputFile.readLine();
			  	setSsn(str);
			  	str = inputFile.readLine();
			  	setSchool(str);
			  	str = inputFile.readLine();
				setGpa(Integer.parseInt(str));
				str = inputFile.readLine();
		        setCredits(Integer.parseInt(str));
				str = inputFile.readLine();
				major = str;
			} // end if
		} //end if
	}
	public void closeInFile(BufferedReader inputFile)throws IOException {
		inputFile.close();
	} // end closeInFile
	public void printReadFromFile(){
		System.out.println(getName());
		System.out.println(getSsn());
		System.out.println(getSchool());
		System.out.println(getGpa());
		System.out.println(getCredits());
		System.out.println(major);
	}
	
	public Object storeToVector(){
		Vector<Object> CollegeStudent = new Vector<Object>();
		int size = 0;
		
		CollegeStudent.add(getName());
		size++;
		CollegeStudent.add(getSsn());
		size++;
		CollegeStudent.add(getSchool());
		size++;
		CollegeStudent.add(getGpa());
		size++;
		CollegeStudent.add(getStatus());
		size++;
		CollegeStudent.add(getCredits());
		size++;
		CollegeStudent.add(major);
		size++;
		CollegeStudent.add(degree);	
		size++;
		
		
		CollegeStudent.setSize(size);
		return CollegeStudent;
		
	}
  }
import java.io.*;
import java.util.Scanner;
public class Driver {

	/**
	getFileName() - gets name of file for reading from or writing to
	@return filename - returns the file name entered by user
*/
public static String getFileName() {
	Scanner scan = new Scanner(System.in);
	String filename;

	System.out.print("Enter the file name and path ==> ");
	filename = scan.nextLine();
	System.out.println("");
	
	return filename;
} // end getFileName

public static void main(String [ ] argv) throws IOException {
	
	String filename = getFileName();
	CollegeStudent temp;
	
	
     

	FileReader freader = new FileReader(filename);
	BufferedReader inputFile = new BufferedReader(freader);
	int flag = 1;
	
	do {
		temp = new CollegeStudent();
		temp.readFromFile(inputFile);

		if (temp.getName().compareTo("") != 0) {
			temp.storeToVector();
			temp.printReadFromFile();
					  
		} else 
			flag = 0;
	} while( flag != 0);
	 
	

	// close file
	temp.closeInFile(inputFile);
		
} 
}

This post has been edited by japanir: 06 April 2011 - 04:50 AM
Reason for edit:: Code Tags Added


Is This A Good Question/Topic? 0
  • +

Replies To: reading from file and store in Vector in java

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 05:04 AM

I don't see any setGpa()-Method which is called in line 75.

Also don't put two classes in one File, use one File for one Class (unless you have a very good reason for using a inner class).

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#3 ytf1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-April 11

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 06:47 AM

luke, thank you very much for the reply.
this is heritance, person, student, Collegestudent, the error has nothing to do with person, student class, so I just post CollegeClass and driver class,in fact this two classes are in separate file. if you could help solve my problem, it will be really appreciated.
[='MrLuke187' date='06 April 2011 - 05:04 AM' timestamp='1302091474' post='1303061']
I don't see any setGpa()-Method which is called in line 75.

Also don't put two classes in one File, use one File for one Class (unless you have a very good reason for using a inner class).

Greetings: Luke
[/quote]
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,069
  • Joined: 27-December 08

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 08:14 AM

There is no Double.parseDouble() call in your readFromFile() method. We need to see your Student class.

Also, can you post some sample inputs in the body of your post so we can test your code?
Was This Post Helpful? 0
  • +
  • -

#5 ytf1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-April 11

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 10:35 AM

here attched all the class (person,student,collegestudent and driver). I changed double.parseDouble(str)
into Integer.parseInteger(str) after got the error message. now I changet it back to Double again. the input text as:
Susan Smith
100-23-2345
University of Texas
2.7
30
CS
Diane Lee
333-44-5555
Community College
3.7
140
Philosophy
David King
555-11-7777
Amhst College
4.0
62
Physics
import java.util.*;
public class Person {
  private String name;
  private String ssn;
  
  static protected Scanner input = new Scanner(System.in);
  
  // Default constructor
  public Person(){
	name = "";
	ssn = "";
  }
  // Primary constructor
  public Person(String n, String s){
	name = n;
	ssn = s;
  }
  
  // accessor for name
  public String getName(){
	return name;
  }
  
  // accessor for ssn
  public String getSsn(){
	return ssn;
  }
  
  // mutator for name
  public void setName(String n){
	name = new String(n);
  }
  
 // mutator for ssn
  public void setSsn(String s){
	ssn = s;
  }
  
  public void read(){
	System.out.print("Enter student name: ");
	 name = input.nextLine();
	 	
	System.out.print("Enter Social Security Number: ");
	ssn = input.nextLine();
	
  }
    public void display(){
	System.out.println("Student name: "+ name);
	System.out.println("Social Security Number: " + ssn);
  }
  
}
public class Student extends Person {
	private String school;
	private double gpa;
	private int credits;
	private String status;
	
	// default constructor
	public Student(){
	   school = null;
	   gpa = 0;
	   credits = 0;
	   status = null;		
	}
	// primary constructor
	public Student(String sch, double gp, int cr, String st){
		school = sch;
		gpa = gp;
		credits = cr;
		status = st;		
	}
	//
	public String getSchool(){
		return school;
	}
	//
	public double getGpa(){
		return gpa;
	}
	//
	public int getCredits(){
		return credits;
	}
    //
	public String getStatus(){
		return status;
	}
	//
	public void setSchool(String sch){
		school = sch;
	}
	//
	public void setGpa(double gp){
		gpa = gp;
	}
	//
	public void setCredits(int cr){
		credits = cr;
	}
	public void setStatus(String st){
		status = st;
	}
	
	//
	public void read(){
		super.read();
		
		System.out.print("Enter the school: ");
		
		school = input.nextLine();
		
		System.out.print("Enter the GPA: ");
		gpa = input.nextDouble();
		
		System.out.print("Enter Credits: ");
		credits = input.nextInt();
		if (credits > 0 && credits < 29){
			status = "freshman";
		}
		else if (credits >30 && credits < 59){
			status = "sophomore";
		}
		else if (credits > 60 && credits < 89){
			status = "junior";
		}
		else{
			status = "senior";
		}
	  
	}
	public void dispaly(){
	    super.display();
	    System.out.println("School: " + school);
	    System.out.println("Freshman Grade Point Average: " + gpa);
	    System.out.println("Credits: " + credits);
	    System.out.println("Status: " + status);
	    
	}
}
import java.io.*;
import java.lang.String;
import java.util.Vector;
public class CollegeStudent extends Student{
	private String degree; 
	private String major;
	
	public CollegeStudent(){
		degree = null;
		major = null;
	}
	//
	
	public CollegeStudent(String dg, String mj){
		degree = dg;
		major = mj;
	}
	
	public String getDegree(){
		return degree;
	}
    //
	public String getMajor(){
		return major;
	}
	//
	public void setDegree(String dg){
		degree = dg;
	}
	
	//
	public void setMajor(String mj){
		major = mj;
	}
	//
	public void read(){
		
		 super.read();
			System.out.print("Enter the major: ");
		    major = input.nextLine();
		    major = major.toLowerCase();
	}
	//
	public void setDegree(){
		
		 String a = "cs engineering math biology chemistry physics";
		 int index = a.indexOf(major);
		 if (index != -1){
			 degree = "BS";
		 }
		 else {
			 degree = "BA";
		 }
	}
	//
	public void display(){
		
		super.dispaly();
		System.out.println("Major: " + major);
		System.out.println("Expected Degree: " + degree);
		System.out.println("");
	}
	public void readFromFile(BufferedReader inputFile) throws IOException {
		String str = "";
	   if (inputFile != null){	
			str = inputFile.readLine();
			if(str != null) {
				setName(str);
				
			  	str = inputFile.readLine();
			  	setSsn(str);
			  	str = inputFile.readLine();
			  	setSchool(str);
			  	str = inputFile.readLine();
				setGpa(Double.parseDouble(str));
				str = inputFile.readLine();
		        setCredits(Integer.parseInt(str));
				str = inputFile.readLine();
				major = str;
			} // end if
		} //end if
	}
	public void closeInFile(BufferedReader inputFile)throws IOException {
		inputFile.close();
	} // end closeInFile
	public void printReadFromFile(){
		System.out.println(getName());
		System.out.println(getSsn());
		System.out.println(getSchool());
		System.out.println(getGpa());
		System.out.println(getCredits());
		System.out.println(major);
	}
	
	public Vector storeToVector(){
		Vector<Object> CollegeStudent = new Vector<Object>();
		int size = 0;
		
		CollegeStudent.add(getName());
		size++;
		CollegeStudent.add(getSsn());
		size++;
		CollegeStudent.add(getSchool());
		size++;
		CollegeStudent.add(getGpa());
		size++;
		CollegeStudent.add(getStatus());
		size++;
		CollegeStudent.add(getCredits());
		size++;
		CollegeStudent.add(major);
		size++;
		CollegeStudent.add(degree);	
		size++;
		
		
		CollegeStudent.setSize(size);
		return CollegeStudent;
		
	}
  }
import java.util.Scanner;
public class Driver {

	/**
	getFileName() - gets name of file for reading from or writing to
	@return filename - returns the file name entered by user
*/
public static String getFileName() {
	Scanner scan = new Scanner(System.in);
	String filename;

	System.out.print("Enter the file name and path ==> ");
	filename = scan.nextLine();
	System.out.println("");
	
	return filename;
} // end getFileName

public static void main(String [ ] argv) throws IOException {
	
	String filename = getFileName();
	CollegeStudent temp;
	
     

	FileReader freader = new FileReader(filename);
	BufferedReader inputFile = new BufferedReader(freader);
	int flag = 1;
	
	do {
		temp = new CollegeStudent();
		temp.readFromFile(inputFile);

		if (temp.getName().compareTo("") != 0) {
			temp.storeToVector();
			temp.printReadFromFile();
					  
		} else 
			flag = 0;
	} while( flag != 0);

} // end main
} // end PersonDriver


Thank you very much!
I am looking forward for your feedback.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,069
  • Joined: 27-December 08

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 04:39 PM

I ran your code and it worked fine for me. A couple of suggestions though. First, don't mix your program state/data and user interface. Your Person class (and its child classes) should not be dealing with reading in Files or console input. Separate classes should handle this, and use the appropriate getter/setter methods on the Person objects.

Second, rather than a display() method, overriding the toString() method adheres to industry standards more. That way, you can just System.out.println(somePersonObject); and the appropriate data will be output to the console.
Was This Post Helpful? 1
  • +
  • -

#7 ytf1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-April 11

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 05:36 PM

I really appreciate for your effort on running my code and the suggestions are really valuable to me. I also can run it and diplay the results(I use eclipes),however, I also got error message as I posted.

can I ask you another question? this project also need read data fields from keyboard and store into vector.I have the read() and StoreToVector() in collegeStudent class. I need a loop in driver class to control reading, such as read mmultiple people's information(name,GPA, Major).I could not figure out how to do it, because some data fields inheritance from supperclass. it will be really appreciated if you could give me any clues.

View Postmacosxnerd101, on 06 April 2011 - 04:39 PM, said:

I ran your code and it worked fine for me. A couple of suggestions though. First, don't mix your program state/data and user interface. Your Person class (and its child classes) should not be dealing with reading in Files or console input. Separate classes should handle this, and use the appropriate getter/setter methods on the Person objects.

Second, rather than a display() method, overriding the toString() method adheres to industry standards more. That way, you can just System.out.println(somePersonObject); and the appropriate data will be output to the console.

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,069
  • Joined: 27-December 08

Re: reading from file and store in Vector in java

Posted 06 April 2011 - 06:30 PM

Quote

I could not figure out how to do it, because some data fields inheritance from supperclass.

So are the getter/setter methods. If a non-private method is defined in Person, you can still use it in CollegeStudent as if it was originally defined in the child class. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1