| | |
Java Arraylist Help Please
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 29
Reputation:
Solved Threads: 0
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
i did want it to print out the customers name,address and number. any help will be much apperciated thanks
i have used the follwing code to print it out of my iterator
Java Syntax (Toggle Plain Text)
System.out.print("contents of customer: "); Iterator<Customer> cus = customerlist.iterator(); while (cus.hasNext()) { Customer element = cus.next(); System.out.print(element + " "); } System.out.println();
Last edited by Superstar288; Dec 13th, 2008 at 8:35 pm.
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
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...
And here is the class with the Getters that actually extract the variable values...
Hope this helps!
Tyster
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)
public class GradeTester { /** * @param args */ public static void main(String[] args) { ArrayList<Grader> GraderList = new ArrayList<Grader>(); Scanner in = new Scanner(System.in); System.out.println("Enter the number of students: "); String numStudents = in.next(); // Store the number of students int numStu = Integer.valueOf(numStudents); //Convert String to int System.out.println("Enter the Assignment name: "); String assignName = in.next(); // Store the assignment name for(int i = 0; i < numStu; i++){ System.out.println("Enter the student name: "); String studentName = in.next(); // Store the student name System.out.println("Enter the student ID: "); String studentID = in.next(); // Store the student ID System.out.println("Enter the student score: "); String score = in.next(); // Store the student score int studentScore = Integer.valueOf(score); //Convert Grader graderObj = new Grader(assignName, studentScore, studentID, studentName); GraderList.add(graderObj); } Collections.sort(GraderList); ListIterator<Grader> iterator = GraderList.listIterator(); iterator = GraderList.listIterator(); while (iterator.hasNext()) System.out.println("The scores are: " + iterator.next().getScore()); //Display ordered scores //Display each value for each grader object for(Grader grader : GraderList){ System.out.println(grader.getAssignmentName() + grader.getStudentID() + grader.getStudentName() + grader.getScore()); }
And here is the class with the Getters that actually extract the variable values...
Java Syntax (Toggle Plain Text)
public class Grader implements Comparable { String assignmentName; String studentID; String studentName; int score; /** * @param assignmentName * @param score * @param studentID * @param studentName */ public Grader(String assignmentName, int score, String studentID, String studentName) { super(); this.assignmentName = assignmentName; this.score = score; this.studentID = studentID; this.studentName = studentName; } public String getAssignmentName() { return assignmentName; } public String getStudentID() { return studentID; } public String getStudentName() { return studentName; } public int getScore() { return score; }
Hope this helps!
Tyster
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
in Customer class override method
public String toString(){
return //..... all data about customer
}•
•
•
•
thanks very much m8 i will try this 2moro hope i can get it 2 work thanks
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.
"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 ?"
"How to ask questions the smart way ?"
Whenever you call this:
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
System.out.print(element); The toString() method of the object is automatically called. These 2 are exactly identical: Java Syntax (Toggle Plain Text)
System.out.print(element); 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
![]() |
Similar Threads
- How to pass an ArrayList from Servlet to JSP? (JSP)
- java.lang.ArrayIndexOutOfBoundsException (Java)
- ArrayList Help (Java)
- How to use collection in java (Java)
- Need Help with ArrayList sorting (Java)
- I need an exampls program in java to store image datatype in ms-sql (MS SQL)
- I/O Filestreams in Java (Java)
- Linked arraylist (Java)
Other Threads in the Java Forum
- Previous Thread: LinkedList
- Next Thread: Editing Lines in Java
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat chatprogramusingobjects class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows






