6 Replies - 2357 Views - Last Post: 02 December 2009 - 05:30 PM Rate Topic: -----

#1 dradford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 27-August 09

Strange output @ca0b6

Posted 02 December 2009 - 11:27 AM

This program compiles but does not output what it is supposed to. Instead of printing out the name of the songs and the artist it prints [LSong;@ca0b6 where each song and artist is supposed to be.

The class
/** This class represents a song */
public class Song
{
	/** The title of the song */
	private String title;
	
	/** The artist who sings the song */
	private String artist;
	
	/** constructor
	@param title The title of the song
	@param artist The artist who sings the song
	*/
	public Song(String title , String artist)
	{
		title=title;
		artist=artist;
	}
	
	/** toString method returns a description of the song
	@return a String containing the name of the song
	and the artist
	*/
	public String toString(String title, String artist)
	{
		return title + " by " + artist + "\n";
	}
}


The main method
/** This program creates a list of songs for a CD by
reading from a file
*/
import java.io.*;
import java.util.Scanner;
/** This program creates a list of songs for a CD by
reading from a file
*/
public class CompactDisc
{
	public static void main(String [] args) throws IOException
	{
		File file = new File("Classics.txt");
		Scanner input = new Scanner(file);
		
		String title;
		String artist;
		
		//Declare an array of songs named cd, of size 6
		Song[] cd=new Song[6];
		
		for (int i = 0; i < cd.length; i++)
		{
			title = input.nextLine();
			artist = input.nextLine();
			cd[i]=new Song(title, artist);
			// fill the array by creating a new song with
			// the title and artist and storing it in the
			// appropriate position in the array
		}
		input.close();
		
		System.out.println("Contents of Classics:");
		
		for (int i = 0; i < cd.length; i++)
		{
			//print the contents of the array to the console
			System.out.println(cd.toString());
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Strange output @ca0b6

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2694
  • View blog
  • Posts: 11,360
  • Joined: 20-September 08

Re: Strange output @ca0b6

Posted 02 December 2009 - 11:36 AM

Quote

public String toString(String title, String artist)


wont be called. The method prototype must be

public String toString()


Was This Post Helpful? 0
  • +
  • -

#3 dradford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 27-August 09

Re: Strange output @ca0b6

Posted 02 December 2009 - 12:16 PM

I made the change to eliminate the parameters but it still has the same output.
Was This Post Helpful? 0
  • +
  • -

#4 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: Strange output @ca0b6

Posted 02 December 2009 - 12:31 PM

System.out.println(cd.toString());



calls the toString method on the Array, not on each object in the array. For that, try

System.out.println(cd[i].toString());


This post has been edited by noctolater: 02 December 2009 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2694
  • View blog
  • Posts: 11,360
  • Joined: 20-September 08

Re: Strange output @ca0b6

Posted 02 December 2009 - 12:35 PM

Quote

I made the change to eliminate the parameters but it still has the same output.


That's good. You should now be able to call

System.out.println(java.util.Arrays.toString(cd));


Was This Post Helpful? 0
  • +
  • -

#6 dradford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 27-August 09

Re: Strange output @ca0b6

Posted 02 December 2009 - 12:59 PM

Thanks to both of you it is now working great. I made all the changes you suggested and I also changed the constructor from
public Song(String title, String artist)
{
	 title=title;
	 artist=artist;
}




To this
public Song(String songTitle , String songArtist)
	{
		title=songTitle;
		artist=songArtist;
	}
	


and it works like its supposed to now
Was This Post Helpful? 0
  • +
  • -

#7 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: Strange output @ca0b6

Posted 02 December 2009 - 05:30 PM

you can also change your constructor to

public Song(String title, String artist)
{
	 this.title=title;
	 this.artist=artist;
}



and it will work as well. The 'this' tells Java you are talking about any object of the class you are in. Basically then, this.title makes it refer to the title instance variable of the object. Before, when it was just title = title, you were setting the title you took in in the constructor to the title you took in in the constructor.

I hope that made sense, implicit this statements always were a little hard for me to explain.

This post has been edited by noctolater: 02 December 2009 - 05:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1