943,186 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2522
  • Java RSS
Apr 11th, 2010
0

JAVA Problems with inserting arraylist object into another object.

Expand Post »
I am working on a project where I am building a student object from student java class. The student.java class specifies a method of:

java Syntax (Toggle Plain Text)
  1. public void setCourses(ArrayList<Course> courses) {
  2. this.courses = courses;
  3. }

So I have built the following two methods in my main.java class file:

java Syntax (Toggle Plain Text)
  1. public static void collectCourseInformation() {
  2. ArrayList<Course> courses = new ArrayList<Course>();
  3. boolean cont = true;
  4. while (cont) {
  5. // Input Course Information
  6. String course = JOptionPane.showInputDialog("Course Name: ");
  7. String grade = JOptionPane.showInputDialog("Grade: ");
  8. String status = JOptionPane.showInputDialog("Status: ");
  9. Double gradeDouble = Double.parseDouble(grade);
  10. Course myCourse = new Course();
  11. myCourse.setCourse(course);
  12. myCourse.setGrade(gradeDouble);
  13. myCourse.setStatus(status);
  14. courses.add(myCourse);
  15.  
  16. int confirm = JOptionPane.showConfirmDialog(null, "Do you want to add more courses to this student?");
  17. if (confirm > 0) {
  18. cont = false;
  19. }
  20. }
  21. }
  22.  
  23. public static void collectStudentInformation() {
  24. ArrayList<Student> list = new ArrayList<Student>();
  25.  
  26. boolean cont = true;
  27. while (cont) {
  28. // Input Student Record Details
  29. String name = JOptionPane.showInputDialog("Name: ");
  30. String major = JOptionPane.showInputDialog("Major: ");
  31. String age = JOptionPane.showInputDialog("Age: ");
  32. String gpa = JOptionPane.showInputDialog("GPA: ");
  33. String ssn = JOptionPane.showInputDialog("Social Security Number: ");
  34.  
  35. // Convert strings to usable data
  36. int ageint = Integer.parseInt(age);
  37. Double gpadouble = Double.parseDouble(gpa);
  38.  
  39. collectCourseInformation();
  40.  
  41. // Create the myStudent object and add it to the arraylist.
  42. Student myStudent = new Student();
  43. myStudent.setCourses(courses);
  44. myStudent.setName(name);
  45. myStudent.setMajor(major);
  46. myStudent.setAge(ageint);
  47. myStudent.setGpa(gpadouble);
  48. myStudent.setSsn(ssn);
  49. list.add(myStudent);
  50.  
  51. int confirm = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
  52. if (confirm > 0) {
  53. cont = false;
  54. }
  55. }
  56. int search = JOptionPane.showConfirmDialog(null, "Do you want to search?");
  57. if (search == 0) {
  58. String searchname = JOptionPane.showInputDialog("Which name?");
  59. searchStudentInformation(list, searchname);
  60. }

The problem is with the line 43. I am essentially creating an array list name myStudent and then populating student data and the collectStudentInformation method then calls the collectCourseInformation to gather the students courses into objects and then fill an arraylist object named courses. I am trying to insert that arraylist object (courses) into the object for myStudent which will then be added to an arraylist named list.

It doesn't want to let me add the courses arraylist object to the student object.

Any ideas on what I am doing wrong. Please note that I am a newbie to Java and I apologize if it's something incredibly stupid that I'm overlooking. LOL Any help would be appreciated.
Last edited by brianmack; Apr 11th, 2010 at 4:04 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brianmack is offline Offline
2 posts
since Apr 2010
Apr 11th, 2010
0
Re: JAVA Problems with inserting arraylist object into another object.
You declared the variable courses from within the method collectCourseInformation. Therefore it doesn't exist outside of that method. Read about variable scope in Java.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 12th, 2010
0
Re: JAVA Problems with inserting arraylist object into another object.
As mentioned in previous reply, the courses variable is local to the method.

You can change the collectCourseInformation method to return the course:
Java Syntax (Toggle Plain Text)
  1. public static Course collectCourseInformation() {
  2. ArrayList<Course> courses = new ArrayList<Course>();
  3. .....
  4. return courses;
  5. }

Then change line 39 to:
Java Syntax (Toggle Plain Text)
  1. Course courses = collectCourseInformation();
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zloiadun is offline Offline
3 posts
since Apr 2010
May 17th, 2010
0
Re: JAVA Problems with inserting arraylist object into another object.
Tank you for this infos to the arraylist, for other germans i have found here a good articel in this blog
http://www.servervoice.de/java-array...mische-arrays/
Last edited by Tecko; May 17th, 2010 at 5:51 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tecko is offline Offline
1 posts
since May 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jbutton array
Next Thread in Java Forum Timeline: ChatServer (Very simple) Help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC