Results 1 to 5 of 5
  1. #1
    nivekeleets is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Cant seem to get this to work

    Hi all i am in a java 2 class. I am working on a final project and i am stuck and cant figure out what is going on at one point. Here is the problem i am writing a program to search though a binary file display the student thier major and thier student id. I have created a class that is a person first name and last and then extended that class with a student class that contains a major and a student id. We are to start the program with a seed file the seed file is csv format. we are to read this file into the program and store each student as an object of the class student and store it into an arraylist. My problem is that when i go to check that all students are in the array. Only one student the last one is stored as each array element. Below is my main tester and two classes the csv file is just a sample of five students in the format first name, last name, major, student id

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class testeR {
    
    	public static void main(String[] args)throws IOException{
    		//variables
    		String lastName, firstName, major,str = "";
    		String var;
    		int studentID,x=0;
    		//input 
    		Scanner keyboard = new Scanner(System.in);
    		//object of student
    		studenT test = new studenT();
    		//create objects of seed data to be stored in array
    		studenT seed = new studenT();
    		studenT test2= new studenT();
    		//studenT seedTest = new studenT();
    		//end variables
    	
    		//array list for initial seed data
    		ArrayList<studenT> seeDA = new ArrayList<studenT>();
    		
    		
    		//open seed file
    		File file = new File("students_raw.csv");
    		Scanner inputFile = new Scanner(file);
    		
    		
    		//get input from seed file
    		while(inputFile.hasNext()){
    			str+= inputFile.next();
    		}//end while
    		
    		//string using split 
    		String[] tokens = str.split(",");
    		
    	
    		for(int i=0;i<=4;i++){
    		String n;
    	    seed.setFirstName(tokens[x]);
    	    x++;
    	    seed.setLastName(tokens[x]);
    	    x++;
    	    seed.setMajor(tokens[x]);
    	    x++;
    	    n=tokens[x];
    	    seed.setStudentID(Integer.parseInt(n));
    	    x++;
    	    seeDA.add(i, seed);
    		}//end
    		
    	    
    	    
    		test2=seeDA.get(2);
    		System.out.println(test2.getFirstName());
    	
    	}//end 
    
    }//end

    Java Code:
    person class
    
    
    public class persoN {
    		private String firstN,lastN;
    		
    	
    	//default constructor
    	public persoN(){
    		firstN = "null";
    		lastN = "null";
    	}//end person
    	
    	public void setFirstName(String f){
    		firstN = f;
    	}//end 
    	
    	public void setLastName(String l){
    		lastN = l;
    	}//end 
    	
    	public String getFirstName(){
    		return firstN;
    	}//end
    	
    	public String getLastName(){
    		return lastN;
    	}//end
    	
    	
    }//end class

    student class

    Java Code:
    public class studenT extends persoN {
    	
    	private int studentID;
    	private String majoR;
    	
    	//default constructor
    	public studenT()
    		{
    		super();
    		studentID =0;
    		majoR = "null";
    	}//end
    	
    	//set student id
    	public void setStudentID(int s){
    		studentID = s;
    	}
    	//set student major
    	public void setMajor(String m){
    		majoR = m;
    	}
    	
    	//get student id
    	public int getStudentID(){
    		return studentID;
    	}//end get
    	
    	//get student major
    	public String getMajor(){
    		return majoR;
    	}//end get
    	
    	}//end class
    Mod edit: code tags added
    Last edited by Fubarable; 11-12-2010 at 08:10 PM. Reason: Mod edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum. I added code tags to your post so your code will retain its formatting and be more readable.

    Your problem may be coming from your reading through your file in the while loop, but discarding the Strings read, not doing anything with them in the loop, and then acting only on the last String after the loop has been completed. Solution: do this logic IN the loop.

    Also, please fix your creative indentation/formatting so we can better understand your posted code.

    Much luck and again welcome.

  3. #3
    nivekeleets is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your help I found the error I forgot to new the extended class at the end of my for loop all of the other code is working. Just one question when you said
    Also, please fix your creative indentation/formatting so we can better understand your posted code.

    This is the only way i know to code my java prof is sort of crazy and has his own way of things I am guessing you work in some sort of programing feild what is an example I could look at of doing things properley.
    thanks kevin
    seed=new studenT();

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

Similar Threads

  1. constructor won't work.
    By nolsen01 in forum New To Java
    Replies: 3
    Last Post: 09-13-2010, 05:55 PM
  2. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  3. Need help with school work
    By ccfdet in forum New To Java
    Replies: 3
    Last Post: 08-29-2009, 07:14 PM
  4. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  5. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •