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

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();

i did want it to print out the customers name,address and number. any help will be much apperciated thanks

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

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

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

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

Cheers!

Tyster

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

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.

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

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

sorry about the slang it was getting late and i just ended up using it sorry if it offended anyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.