Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    printing an Array in alphabetical order

    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:
    Code:
       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!

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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 to write your own sort

    2) Comparable writing

    If you have any issues, let us know!
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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:

    Code:
     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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •