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 2 of 2

Thread: Passing arrayList inside class

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Dublin
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Passing arrayList inside class

    Hello.

    I'm having a problem with a program i'm writing at the moment.
    The program should store information about students(name, surname, module, grades etc.)
    So i created a class of type Student that will store all this information, and another class SystemM that contains methods for adding students, removing, viewing details and loading and saving to file.

    But when i create a new student in my GUI class and send it to the System class i can't access this object.
    Let me post my code.

    package modulegradebook;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
     
    /**
     *
     * @author Kris_the_Savage
     */
    public class SystemM {
     
    	private PrintStream			outFile;
    	private FileReader			inFile;
    	private ArrayList<Student>	student = new ArrayList<Student>();
    	private ArrayList			grade = new ArrayList();
    	//private ArrayList<User>		user;
     
     
    	SystemM(){}
    //*****************************************************************************
    //this method will save created students to file
    //
    //@param	none
    //
    //return	none
    //*****************************************************************************
    	public void saveStudentToFile(){
    		try {
    			System.out.print("save");
    			outFile = new PrintStream(new File("Students.txt"));
    			for (int i = 0; i < student.size(); i++) {
    //				outFile.write(i); //students id
    				outFile.println(student.get(i).getName());
    				outFile.println(student.get(i).getSurname());
    				outFile.println(student.get(i).getAddres());
    				outFile.println(student.get(i).getModuleName());
    				outFile.println(student.get(i).getGradesArray());
    			}
    		} catch (IOException ex) {
    			System.err.print("Can't open file");
    		}
     
    	}//end of saveStudent method
     
     
    //*****************************************************************************
    //this method will load students from file
    //
    //@param	filename
    //
    //return	ArrayList of type Student wit all students created
    //*****************************************************************************
    	public void loadStudentFromFile(String filename){
    		try {
    			Scanner inFile = new Scanner(new FileReader(filename));
    			while (inFile.hasNext()) {
    				String name = inFile.nextLine();
    				String surname = inFile.nextLine();
    				String addres = inFile.nextLine();
    				String module = inFile.nextLine();
    				while(inFile.hasNextInt()){
     
    					int inGrade = inFile.nextInt();
    					grade.add(inGrade);
    				}
    				Student s = new Student(name, surname, module, addres);
    				student.add(s); //add created student to arraylist
    				s.setGrades(grade);
    				for(int x = 0;x<student.size(); x++){
    				System.out.println("load  " + student.get(x).getName()+student.get(x).getSurname()+student.get(x).getGradesString());
    				}
    			}
     
    			//return student;
    		} catch (FileNotFoundException ex) {
     
    		}
    		//return student;
    	}//end of loadStudentFromFile method
    //*****************************************************************************
    //this method displays details of chosen student
    //called by academics
    //
    //@param	none
    //
    //return	none
    //*****************************************************************************
     
    	public void viewStudentDetails(){
     
    		String vName = JOptionPane.showInputDialog("Enter students name");
     
    		for(int v = 0; v<student.size(); v++){
     
    			System.out.println(student.get(v).getName()
    					   + "\n" + student.get(v).getSurname()
    					   + "\n" + student.get(v).getAddres()
    					   + "\n" + student.get(v).getModuleName());
     
    			//if entered name matches concatenated name and surname of student
    			//then display that students details
    			vName.trim();		//remove space
    			if(vName.equalsIgnoreCase(student.get(v).getName().concat(" ").concat(student.get(v).getSurname()))){
     
    				JOptionPane.showMessageDialog(null, student.get(v).getName()
    					   + "\n" + student.get(v).getSurname()
    					   + "\n" + student.get(v).getAddres()
    					   + "\n" + student.get(v).getModuleName());
     
    			}
    		}
    	}//end of viewStudentDetails
     
    //*****************************************************************************
    //this method will create a student and add it to the arraylist
    //It will be called by admin and only by admin
    //
    //@param	none
    //
    //return	none
    //*****************************************************************************
    	public void addStudent(){
     
    		DetailsGUI d = new DetailsGUI();
     
    	}//end of addd student method
     
     
    //*****************************************************************************
    //this method deletes chosen student from list
    //called by and only by admin
    //
    //@param	none
    //
    //return	none
    //*****************************************************************************
    	public void deleteStudent(){
     
    		String delName = JOptionPane.showInputDialog("Enter name of the student you wish to delete");
     
    		for(int d = 0; d<student.size(); d++){
     
    			//if entered name matches concatenated name and surname of student
    			//then delete that student
    			delName.trim();	//remove space
    			if(delName.equalsIgnoreCase(student.get(d).getName().concat(" ").concat(student.get(d).getSurname()))){
     
    				student.remove(d);
    			}
    		}
    	}//end of deleteStudent method
     
     
     
     
    //*****************************************************************************
    //this method displays marks of all students with their names and surnames
    //can be called by admin and academic
    //
    //@param	none
    //
    //return	none
    //*****************************************************************************
    	public void viewAllStudentsAndMarks(){
     
    		System.out.println("its here");	//just for debbuging
    		for(int i = 0;i<student.size(); i++){
     
    			JOptionPane.showMessageDialog(null, student.get(i).getName()
    				   + " " + student.get(i).getSurname()
    				   + " " + student.get(i).getGradesString()
    				   + "\n");
    		}
    	}//end of viewAllStudentsAndMarks method
     
    //*****************************************************************************
    //Getter methodes
    //*****************************************************************************
    	public ArrayList<Student> getStudentList(){
     
    		return student;
    	}
     
    //*****************************************************************************
    //Setter methodes
    //*****************************************************************************
    	public void setStudent(Student st){
     
    		student.add(st);
     
    		//for debbuging
    		for(int i=0;i<student.size();i++){
    		System.out.print(student.get(i).getName()
    				   + " " + student.get(i).getSurname()
    				   + " " + student.get(i).getModuleName()
    				   + "\n");}
    	}
    }

    This is my SystemM class. When i use the setStudent method i see that student was passed into this class but i can't acces it with any other method. And it seems that adding next object of type Student overwrites the existing one. When next i try call method public void viewAllStudentsAndMarks() it does not enter the loop for which suggests that ArrayList does not exist.

    I would really appreciate help. I'm quite new to programming so if i'm making some stupid mistake please be gentle

    Thanks in advance.
    Last edited by KrisTheSavage; March 26th, 2010 at 02:53 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Dublin
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing arrayList inside class[SOLVED]

    Ok. I solved it.

    My problem was that i was instantiating SystemM class twice in within the scope of the program.

Similar Threads

  1. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM
  2. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 03:40 PM
  3. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  4. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM
  5. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM