Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program which gathers feedback from students. I have stored it in an ArrayList. I have a method named display() which will display the feedback details. But when I call the method it shows other values(reference address or address) not the actual details of the object.** I have only called the 0th element of array for testing.** Below is the code...
-----------------------------------------------------------
Java
import java.util.*;
import java.io.*;
class FeedBack
{
	int enroll;
	String name;
	String feedback;
	String subject;

	public void getFeedback()
	{
		Scanner s=new Scanner(System.in);
		System.out.println("Enter Enrollment No:");
		enroll=s.nextInt();
		System.out.println("Enter Name:");
		name=s.next();
		System.out.println("Enter feedback:");
		feedback=s.next();
		System.out.println("Enter Subject:");
		subject=s.next();
	}
	
	public void display(ArrayList al)
	{
		System.out.println("Given FeedBack....!");
		System.out.println(al.get(0));
		
		
		
		
	}
}

class StudFeedback
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		FeedBack fb,fb1;
		fb1=new FeedBack();
		ArrayList<feedback> al=new ArrayList<feedback>();
	        int c;
		do
		{
			System.out.println("Do you want to give Feedback 1 or 0 ?");
			c=Integer.parseInt(br.readLine());
			if(c==1)
			{
				fb=new FeedBack();
				fb.getFeedback();
				al.add(fb);
			}
			else
			{
				break;			
			}
		}while(c!=0);
		fb1.display(al);
		
	}
}

---------------------------------------------
Posted
Updated 20-Aug-15 22:15pm
v3

Your FeedBack class does not include an override of the toString method. So when you call println on an element of the ArrayList<feedback> list, which is a FeedBack object, it has no idea how to print it, so it just prints the memory address.
 
Share this answer
 
Comments
CPallini 21-Aug-15 4:57am    
Damn, you beat me on time! :-)
5.
Richard MacCutchan 21-Aug-15 4:59am    
I've been awake since 04:30. :(
CPallini 21-Aug-15 5:34am    
Look at the bright side: you might have seen the dawn.
(By the way, I forgot to actually vote your solution, fixed now).
Arasappan 21-Aug-15 5:46am    
:-(
That happens because the default toString implementation provides such a info (that is object type and address). However, you may override it providing (in FeedBack class) your own implementation of the toString method. For example
Java
public String toString()
{
  String result = String.format("Enrollment No: %d\nName: %s\nFeedback: %s\nSubject: %s\n", enroll, name, feedback, subject);
  return result;
}
 
Share this answer
 
Comments
Richard MacCutchan 21-Aug-15 5:02am    
And you get a 5 for providing the code.
CPallini 21-Aug-15 5:36am    
Thank you and have a nap. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900