943,926 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1941
  • Java RSS
Dec 13th, 2008
0

Java Arraylist Help Please

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 13th, 2008
0

Re: Java Arraylist Help Please

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...

Java Syntax (Toggle Plain Text)
  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...

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 27
Solved Threads: 1
Light Poster
Tyster is offline Offline
31 posts
since Oct 2007
Dec 13th, 2008
0

Re: Java Arraylist Help Please

NOTE: The classes I posted above are NOT complete, but I think they have what you are looking for.

Cheers!

Tyster
Reputation Points: 27
Solved Threads: 1
Light Poster
Tyster is offline Offline
31 posts
since Oct 2007
Dec 13th, 2008
0

Re: Java Arraylist Help Please

in Customer class override method
    public String toString(){
        return //..... all data about customer
    }
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 13th, 2008
0

Re: Java Arraylist Help Please

thanks very much m8 i will try this 2moro hope i can get it 2 work thanks
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 14th, 2008
0

Re: Java Arraylist Help Please

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:-
Quote ...
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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 14th, 2008
0

Re: Java Arraylist Help Please

Whenever you call this:
System.out.print(element); The toString() method of the object is automatically called. These 2 are exactly identical:

Java Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Dec 14th, 2008
0

Re: Java Arraylist Help Please

sorry about the slang it was getting late and i just ended up using it sorry if it offended anyone.
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008

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: LinkedList
Next Thread in Java Forum Timeline: Editing Lines in Java





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


Follow us on Twitter


© 2011 DaniWeb® LLC