Java Arraylist Help Please

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Java Arraylist Help Please

 
0
  #1
Dec 13th, 2008
hi i was doing some work for my project in a gui and i wanted to print out the information in my arraylist but when i do print out the object all i get is Customer@197d257

i have used the follwing code to print it out of my iterator
  1. System.out.print("contents of customer: ");
  2. Iterator<Customer> cus = customerlist.iterator();
  3. while (cus.hasNext()) {
  4. Customer element = cus.next();
  5. System.out.print(element + " ");
  6. } System.out.println();
i did want it to print out the customers name,address and number. any help will be much apperciated thanks
Last edited by Superstar288; Dec 13th, 2008 at 8:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: Java Arraylist Help Please

 
0
  #2
Dec 13th, 2008
I have something that might help... When you have an array list of objects, each object has instance variable values. The idea is to extract and print the values of each variable in each object. The way you are doing it now prints the OBJECT (and its memory address, I think), rather than the variable values.

To solve this have a look at the program below. It's a program that sets and gets student grades, names, and student Id's and each student object is stored in an Array List. I wrote it to do exactly what you want (I think!). You need 2 classes... the main() method class to provide values for the variables, and a second class that has Getter methods, where each get() method gets the value you want. You print the values using the get() methods.

Here's the main() class...

  1.  
  2. public class GradeTester {
  3.  
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8.  
  9. ArrayList<Grader> GraderList = new ArrayList<Grader>();
  10.  
  11. Scanner in = new Scanner(System.in);
  12.  
  13. System.out.println("Enter the number of students: ");
  14. String numStudents = in.next(); // Store the number of students
  15. int numStu = Integer.valueOf(numStudents); //Convert String to int
  16.  
  17. System.out.println("Enter the Assignment name: ");
  18. String assignName = in.next(); // Store the assignment name
  19.  
  20. for(int i = 0; i < numStu; i++){
  21.  
  22. System.out.println("Enter the student name: ");
  23. String studentName = in.next(); // Store the student name
  24.  
  25. System.out.println("Enter the student ID: ");
  26. String studentID = in.next(); // Store the student ID
  27.  
  28. System.out.println("Enter the student score: ");
  29. String score = in.next(); // Store the student score
  30. int studentScore = Integer.valueOf(score); //Convert
  31.  
  32. Grader graderObj = new Grader(assignName, studentScore, studentID, studentName);
  33. GraderList.add(graderObj);
  34. }
  35.  
  36. Collections.sort(GraderList);
  37.  
  38. ListIterator<Grader> iterator = GraderList.listIterator();
  39. iterator = GraderList.listIterator();
  40. while (iterator.hasNext())
  41. System.out.println("The scores are: " + iterator.next().getScore()); //Display ordered scores
  42.  
  43.  
  44.  
  45. //Display each value for each grader object
  46. for(Grader grader : GraderList){
  47.  
  48. System.out.println(grader.getAssignmentName() + grader.getStudentID() + grader.getStudentName() + grader.getScore());
  49.  
  50. }

And here is the class with the Getters that actually extract the variable values...

  1.  
  2. public class Grader implements Comparable {
  3.  
  4. String assignmentName;
  5. String studentID;
  6. String studentName;
  7. int score;
  8. /**
  9. * @param assignmentName
  10. * @param score
  11. * @param studentID
  12. * @param studentName
  13. */
  14. public Grader(String assignmentName, int score, String studentID,
  15. String studentName) {
  16. super();
  17. this.assignmentName = assignmentName;
  18. this.score = score;
  19. this.studentID = studentID;
  20. this.studentName = studentName;
  21.  
  22. }
  23.  
  24. public String getAssignmentName() {
  25. return assignmentName;
  26. }
  27.  
  28. public String getStudentID() {
  29. return studentID;
  30. }
  31.  
  32. public String getStudentName() {
  33. return studentName;
  34. }
  35.  
  36. public int getScore() {
  37. return score;
  38. }

Hope this helps!

Tyster
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: Java Arraylist Help Please

 
0
  #3
Dec 13th, 2008
NOTE: The classes I posted above are NOT complete, but I think they have what you are looking for.

Cheers!

Tyster
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Java Arraylist Help Please

 
0
  #4
Dec 13th, 2008
in Customer class override method
    public String toString(){
        return //..... all data about customer
    }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: Java Arraylist Help Please

 
0
  #5
Dec 13th, 2008
thanks very much m8 i will try this 2moro hope i can get it 2 work thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Java Arraylist Help Please

 
0
  #6
Dec 14th, 2008
Originally Posted by Superstar288 View Post
thanks very much m8 i will try this 2moro hope i can get it 2 work thanks
Too much of an effort to type in the full words ???
Let me just quote some of the member rules here:-
We strive to be a community geared towards the professional. We strongly encourage all posts to be in full-sentence English. Please do not use "leet" speak or "chatroom" speak.
Besides being against the rules here and plain annoying, the opposite person can spend more time solving your problem rather that trying to understand what you are actually saying.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Java Arraylist Help Please

 
0
  #7
Dec 14th, 2008
Whenever you call this:
System.out.print(element); The toString() method of the object is automatically called. These 2 are exactly identical:

  1. System.out.print(element);
  2. System.out.print(element.toString());

If you don't override the toString() method in your class the toString method of the super class is called and in your case the toString method of the Object class
Last edited by javaAddict; Dec 14th, 2008 at 7:36 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: Java Arraylist Help Please

 
0
  #8
Dec 14th, 2008
sorry about the slang it was getting late and i just ended up using it sorry if it offended anyone.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC