Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I was trying to display an array of objects so that it will show object information. However I did not get the right result. The result looked like this:
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302

Item class:
Java
private String title;
	private String classification;
	private String genre;
	private int ID;
	private int year;
	private Calendar dateLastReturned;
	private boolean available;
	private RentItem latest;
	
	public Item(int ID, int year, String title, String classification, String genre)
	{
		this.ID = ID;
		this.year = year;
		this.title = title;
		this.classification = classification;
		this.genre = genre;
		dateLastReturned = null;
		available = true;
		latest = null;
	}
	
	public Item()
	{
		ID = 0;
		year = 0;
		title = "";
		classification = "";
		genre = "";
		available = true;
		dateLastReturned = null;
		latest = null;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getClassification() {
		return classification;
	}

	public void setClassification(String classification) {
		this.classification = classification;
	}

	public String getGenre() {
		return genre;
	}

	public void setGenre(String genre) {
		this.genre = genre;
	}

	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public Calendar getDateLastReturned() {
		return dateLastReturned;
	}

	public void setDateLastReturned(Calendar dateLastReturned) {
		this.dateLastReturned = dateLastReturned;
	}

	public boolean isAvailable() {
		return available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}

	public RentItem getLatest() {
		return latest;
	}

	public void setLatest(RentItem latest) {
		this.latest = latest;
	}
	
	public void returnItem(Calendar endDate)
	{
		setDateLastReturned(endDate);
		available = true;
	}

	public String toString() 
	{
		String str = "";
		str= str + "ID: "+ID + "\n"+"Year: "+year+"\n"+"Title: "+title+"\n"+"Classification: "+ classification+"\n"+"Genre: "+genre+"\n";
		str = str +"Return Date: "+retDay+"/"+retMonth+"/"+retYear+"\n"+"Available: "+available+"\n";
		if (getLatest() != null)
			str = str + "Most Recent Rental:"+"\t\t\t\t" +getLatest().getReference();
		return str;
	}


DVD class(This class is the subclass of the Item class):

Java
private int numberOfDiscs;
	private int filmRunTime;
	private String subtitles;
	
	public DVD(int ID, int year, String title, String classification, String genre, int numberOfDiscs, int filmRunTime, String subtitles)
	{
		this.numberOfDiscs = numberOfDiscs;
		this.filmRunTime = filmRunTime;
		this.subtitles = subtitles;
	}
	public DVD()
	{
		numberOfDiscs = 0;
		filmRunTime = 0;
		subtitles = "";
	}
	public int getNumberOfDiscs() {
		return numberOfDiscs;
	}
	public void setNumberOfDiscs(int numberOfDiscs) {
		this.numberOfDiscs = numberOfDiscs;
	}
	public int getFilmRunTime() {
		return filmRunTime;
	}
	public void setFilmRunTime(int filmRunTime) {
		this.filmRunTime = filmRunTime;
	}
	public String getSubtitles() {
		return subtitles;
	}
	public void setSubtitles(String subtitles) {
		this.subtitles = subtitles;
	}
	@Override
	public String toString() {
		return "DVD [numberOfDiscs=" + numberOfDiscs + ", filmRunTime="
				+ filmRunTime + ", subtitles=" + subtitles + ", getTitle()="
				+ getTitle() + ", getClassification()=" + getClassification()
				+ ", getGenre()=" + getGenre() + ", getID()=" + getID()
				+ ", getYear()=" + getYear() + ", getDateLastReturned()="
				+ getDateLastReturned() + ", isAvailable()=" + isAvailable()
				+ ", getLatest()=" + getLatest() + ", toString()="
				+ super.toString() + ", getClass()=" + getClass()
				+ ", hashCode()=" + hashCode() + "]";
	} 


What I have tried:

I have tried the following:

Java
static Item[] productStorage= new Item[2];


Java
public static void initialise()
	{
		//creating objects
		DVD dvd1 = new DVD(1, 2010, "Shutter Islands", "16", "Mystery, Thriller", 1, 138, "French, Polish, English");
		DVD dvd2 = new DVD(2, 2006, "Prestige", "12", "Drama, Mystery", 1, 130, "English, German, French, Spanish");

//Assigning value to the array
		productStorage[0] = dvd1;
		productStorage[1] = dvd2;

for(int i = 0; i < productStorage.length; i++)
		{
			System.out.println(productStorage);
		}

}

However it displays it like that:
[LItem;@55f96302
[LItem;@55f96302


Any help will be appreciated
Thanks!
Posted
Updated 6-May-17 14:22pm
v2
Comments
Member 13081540 6-May-17 18:56pm    
Whenever I added [i] to the statement. It displayed the right variables from the DVD class but not the variables that I have in Item, it did not set the variables instead it only showed the default variable values of the Item class. Not sure how to do it so that it will display all the values I have putted inside the dvd1/2 objects.

Your problem is in this statement:
Quote:
System.out.println(productStorage);
Look carefully at the code around it and you should see what i am referring to...
for(int i = 0; i < productStorage.length; i++)
		{
			System.out.println(productStorage);
		}
 
Share this answer
 
Comments
Member 13081540 6-May-17 17:38pm    
Oh hahah, such a silly mistake :D, thank you for helping. I needed to put [i]
Can you show what you are getting?

What I think it is though is you are only keeping three of the values you are providing in the constructor...
public DVD(int ID, int year, String title, String classification, String genre, int numberOfDiscs, int filmRunTime, String subtitles)
{
    this.numberOfDiscs = numberOfDiscs;
    this.filmRunTime = filmRunTime;
    this.subtitles = subtitles;
}
which means these are the only things that will print... the rest (ID, year title, classification, genre) never assigned so they will be null/default values.
 
Share this answer
 
Comments
Member 13081540 6-May-17 20:41pm    
Hey, thanks for responding, I fixed the issue. I needed to add super(....) in the constructor.

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