I saw a similar problem to this, but couldn't understand the solution the guy used. I have to sort this tunes program in order by title. It seems like there is a problem with my sort algorithm, but I cannot figure it out. Here are the classes.

public class Tunes{


	public static void main(String[] args) {

		CDCollection music = new CDCollection();
		
		music.addCD("Storm Front", "Billy Joel", 14.95, 10);
		music.addCD("Come On Over", "Shania Twain", 14.95, 16);
		music.addCD("Soundtrack", "Les Mirables", 17.95, 33);
		music.addCD("Graceland", "Paul Simon", 13.90, 11);
		System.out.println(music);
		
		music.addCD("Double Live", "Garth Brooks", 19.99, 26);
		music.addCD("Greatest Hits", "Jimmy Buffet", 15.95, 13);
		
		System.out.println(music);
		
	}

}


----------------------------
import java.text.NumberFormat;
public class CDCollection {
	
	private CD[] collection;
	private int count;
	private double totalCost;
	
	public CDCollection()
	{
		collection = new CD[100];
		count = 0;
		totalCost = 0.0;
	}
	public void addCD (String title, String artist, double cost, int tracks)
	{
		if (count == collection.length)
			increaseSize();
		collection[count] = new CD (title, artist, cost, tracks);
		totalCost += cost;
		count++;
	}
	public String toString()
	{
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
		
		String report = "----------------------------------------\n";
		report += "My CD Collection\n";
		
		report += "Number of CDs: " + count + "\n";
		report += "Total cost: " + fmt.format(totalCost) + "\n";
		report += "Average cost: " + fmt.format(totalCost/count);
		
		report +="\n\nCD List:\n\n";
		
		Sort.selectionSort(collection);
		for(int i = 0; i<collection.length;i++)
			report+=collection[i];
		
		return report;
	}
	private void increaseSize ()
	{
		CD[] temp = new CD[collection.length *2];
		
		for (int cd = 0; cd < collection.length;cd++)
			temp[cd] = collection[cd];
		
		collection = temp;
	}

}
-------------------------
import java.text.NumberFormat;

public class CD implements Comparable
{
	private String title, artist;
	private double cost;
	private int tracks;
	
	public CD (String name, String singer, double price, int numTracks)
	{
		title = name;
		artist = singer;
		cost = price;
		tracks = numTracks;
	}
	public String toString()
	{
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
		
		String description;
		
		description = fmt.format(cost) + "\t" + tracks + "\t";
		description +=title + "\t" + artist;
		
		return description;
	}
	public boolean equals (CD other)
	{
		return(title.equals(other.getTitle()));
	}
	public int compareTo(Object other) {
		int result;
		String otherTitle = ((CD)(other)).getTitle();
		result = title.compareTo(otherTitle);
		return result;
	
	}
	public String getTitle()
	{
		return title;
	}

}
----------------------------

public class Sort {

	public static void selectionSort (CD[] list)
	{
		int min;
		CD temp;
		
		for (int index = 0; index <list.length-1; index++)
		{
			min = index;
			for(int scan = index+1; scan<list.length; scan++)
				if(list[scan].compareTo(list[min])<0)
					min = scan;
			
			temp = list[min];
			list[min] = list[index];
			list[index] = temp;
		}
	}
}

Recommended Answers

All 9 Replies

for(int scan = index+1; scan<list.length; scan++)
if(list[scan].compareTo(list[min])<0)
min = scan;

temp = list[min];
list[min] = list[index];
list[index] = temp;

You need to use code tags, i.e., what I used in this post. The problem might be your use of brackets; did you mean to use brackets after the if statement?

No I've used a similar algorithm in another program. The if statement just finds the object in the array with the lowest value and sets it to min. Then after the if statement the object[min] is added to the start of the array. At least this is what it is supposed to do. Do I have to manually add the code tags?

No, you don't need to add anything manually. The button that looks like a # is the code tags button. Then you just put =Java after the first CODE.

And I spent a few minutes looking at your code, but I don't see anything wrong with your sorting algorithm, but it was difficult for me to understand the code, not having used that algorithm before - I'm sure you'll get some more replies if you use the code tags. Good luck.

public class Tunes{


public static void main(String[] args) {

CDCollection music = new CDCollection();

music.addCD("Storm Front", "Billy Joel", 14.95, 10);
music.addCD("Come On Over", "Shania Twain", 14.95, 16);
music.addCD("Soundtrack", "Les Mirables", 17.95, 33);
music.addCD("Graceland", "Paul Simon", 13.90, 11);
System.out.println(music);

music.addCD("Double Live", "Garth Brooks", 19.99, 26);
music.addCD("Greatest Hits", "Jimmy Buffet", 15.95, 13);

System.out.println(music);

}

}


----------------------------
import java.text.NumberFormat;
public class CDCollection {

private CD[] collection;
private int count;
private double totalCost;

public CDCollection()
{
collection = new CD[100];
count = 0;
totalCost = 0.0;
}
public void addCD (String title, String artist, double cost, int tracks)
{
if (count == collection.length)
increaseSize();
collection[count] = new CD (title, artist, cost, tracks);
totalCost += cost;
count++;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

String report = "----------------------------------------\n";
report += "My CD Collection\n";

report += "Number of CDs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);

report +="\n\nCD List:\n\n";

Sort.selectionSort(collection);
for(int i = 0; i<collection.length;i++)
report+=collection[i];

return report;
}
private void increaseSize ()
{
CD[] temp = new CD[collection.length *2];

for (int cd = 0; cd < collection.length;cd++)
temp[cd] = collection[cd];

collection = temp;
}

}
-------------------------
import java.text.NumberFormat;

public class CD implements Comparable
{
private String title, artist;
private double cost;
private int tracks;

public CD (String name, String singer, double price, int numTracks)
{
title = name;
artist = singer;
cost = price;
tracks = numTracks;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

String description;

description = fmt.format(cost) + "\t" + tracks + "\t";
description +=title + "\t" + artist;

return description;
}
public boolean equals (CD other)
{
return(title.equals(other.getTitle()));
}
public int compareTo(Object other) {
int result;
String otherTitle = ((CD)(other)).getTitle();
result = title.compareTo(otherTitle);
return result;

}
public String getTitle()
{
return title;
}

}
----------------------------

public class Sort {

public static void selectionSort (CD[] list)
{
int min;
CD temp;

for (int index = 0; index <list.length-1; index++)
{
min = index;
for(int scan = index+1; scan<list.length; scan++)
if(list[scan].compareTo(list[min])<0)
min = scan;

temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
}

Can't see any obvious error - what exactly goes wrong when you run it?

oh I guess I should of said that first haha.

Exception in thread "main" java.lang.NullPointerException
	at Sort.selectionSort(Sort.java:13)
	at CDCollection.toString(CDCollection.java:35)
	at java.lang.String.valueOf(String.java:2615)
	at java.io.PrintStream.print(PrintStream.java:616)
	at java.io.PrintStream.println(PrintStream.java:753)
	at Tunes.main(Tunes.java:13)

I don't understand how it's a null pointer.

The listing we have doesn't show line 13 as such, so it's not easy.
Identify which line it's talking about, then, just before that, put separate System.out.printlln statements for each and every variable or expression in the offending line. That will tell you which variable/expression is null. After that, it will (probably) become blindingly obvious :-)

oh right. Line 13 is in the sort class. it's the if statement:
if(list[scan].compareTo(list[min])<0)
which is line 133 in my actual post. sorry this is my first time using the forum. And i'm not sure what you mean by putting separate println statements..?

Looks like list[?] is empty. Is it because you are looping thru the whole array, not just the first few elements that have been populated?
When you get 1 element past the last one you populated then you will get an empty element ie null pointer.

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.