...

View Full Version : printing an Array in alphabetical order



AnthonyJK
12-07-2007, 09:39 AM
I've been working on this applet which lets you input CD's and it will print them in a Text Area. Right now I have everything working except them printing in alphabetical order. I have not the slightest clue how to do this. I thought of maybe using a switch statement but I'm not to sure how implement it. I was wondering if anyone could give me a nudge in the right direction

Here is my applet and code so you can understand what I mean:

http://www2.hawaii.edu/~akinsey/CDStorage.html

http://www2.hawaii.edu/~akinsey/CDStorage.java

http://www2.hawaii.edu/~akinsey/CDStorage.java

Here are the parts that I would assume need to be edited because both print the array:


public void removeAlbum(CD album){
String sCurrentList = "";
boolean bBlank = false;
taStorage.setText("");

for(int i=0; i<=iArraySize; i++){
if(album != arrStorage[i]){
if(arrStorage[i].getArtist().equals("") && arrStorage[i].getTitle().equals("")){
bBlank = true;
}
if(!bBlank){
if(taStorage.getText().equals("")){
sCurrentList = arrStorage[i].toString();
taStorage.setText(sCurrentList);
}
else{
sCurrentList = sCurrentList + "\n" + arrStorage[i].toString();
taStorage.setText(sCurrentList);
}
}
else{
bBlank = false;
} //ends else
} //ends if
} //ends for
}

public void printStorage(String artist, String title){
String sPrint = "";
String sUpdate = "";
boolean bIsBlank = false;
taStorage.setText("");
arrStorage[iArraySize] = new CD(artist, title);
sPrint = arrStorage[iArraySize].toString();
for(int iASize=0; iASize<=iArraySize; iASize++){
if(arrStorage[iASize].getArtist().equals("") && arrStorage[iASize].getTitle().equals("")){
bIsBlank = true;
}
if(!bIsBlank){
if(taStorage.getText().equals("")){
sUpdate = arrStorage[iASize].toString();
taStorage.setText(sUpdate);
}
else{
sUpdate = sUpdate + "\n" + arrStorage[iASize].toString();
taStorage.setText(sUpdate);
}
}
else{
bIsBlank = false;
} //ends else
}
//Every time this runs the array size increases
iArraySize++;
}
} //close class



Any help would be greatly appreciated. Thanks in advance!

Aradon
12-07-2007, 04:05 PM
You can do one of two things.

1) Create your own sort and sort the CD's in alphabetical order then print them

2) Implement the comparable interface and then write a compareTo method in your CD's. Giving the CD's a natural order by name. Then calling Arrays.sort(arrStorage);

1) Insertion Sort (http://www.dreamincode.net/code/snippet516.htm) to write your own sort

2) Comparable (http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html) writing

If you have any issues, let us know!

AnthonyJK
12-07-2007, 08:18 PM
Wow, thanks a bunch. I'm going to look into those now and I'll get back to you if I can't figure it out. Again thanks!

AnthonyJK
12-07-2007, 10:10 PM
Hey, so I've been working on this for the past few hours. I tried to write a comparator class and call it before the array gets printed.

Here is my CDComparator.java


import java.util.Comparator;

public class CDComparator implements Comparator<CD> {
public int compare(CD myCD, CD anothermyCD) {
String artist1 = ((CD) myCD).getArtist().toUpperCase();
String title1 = ((CD) myCD).getTitle().toUpperCase();
String artist2 = ((CD) anothermyCD).getArtist().toUpperCase();
String title2 = ((CD) anothermyCD).getTitle().toUpperCase();
if(!(title1.equals(title2))){
return title1.compareTo(title2);
}
else{
return artist1.compareTo(artist2);
}
}
}


And I call it in my CDStorage.java when I print the array:



public void printStorage(String artist, String title){
String sUpdate = "";
boolean bIsBlank = false;
taStorage.setText("");
arrStorage[iArraySize] = new CD(artist, title);
Arrays.sort(arrStorage, new CDComparator());
for(int iASize=0; iASize<=iArraySize; iASize++){
if(arrStorage[iASize].getArtist().equals("") && arrStorage[iASize].getTitle().equals("")){
bIsBlank = true;
}
if(!bIsBlank){
if(taStorage.getText().equals("")){
sUpdate = arrStorage[iASize].toString();
taStorage.setText(sUpdate);
}
else{
sUpdate = sUpdate + "\n" + arrStorage[iASize].toString();
taStorage.setText(sUpdate);
}
}
else{
bIsBlank = false;
} //ends else
}
//Every time this runs the array size increases
iArraySize++;
}


Any idea why when I run my applet now it won't print anything at all?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum