Question

Ok i have a Swing app that I'd like to be able to sort my arrays. I have built the sort method I just need the condition trigger to work. For some reason I can't figure out how to get the sort by JMenuBar items to trigger it. As you can see in my code I have made attempts to get it to work to but can't seem to make it happen. The switch and if statements are commented out so the app will run. Now that everything else is working I would like to make the sort by feature work.Thanks in advance Guys!

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.text.*;

public class YourMusic extends JFrame implements ActionListener
{

 //construct components
JTextPane textPane = new JTextPane();



//initalize data in arrays
 String artist[]={"Eminem", "Britney S", "Dre" };
 String album[]={"Eminem Show", "Her Album", "Cronic" };
 String genre[]={"Rap", "Pop", "Hip-hop" };
 String hit[]={"Lose Yourself", "Toxic", "D.R.E" };
 String lable[]={"Interscope", "Britney Bitch", "Aftermath" };


//construct an instance of YourMusic
public YourMusic()
{
    super("Your Music");
}

 //create the menu system
public JMenuBar createMenuBar()
 {
   ///create instance of menu bar
    JMenuBar mnuBar =new JMenuBar();
    setJMenuBar(mnuBar);

    //Construct and Populate the File menu
    JMenu mnuFile = new JMenu("File",true);
        mnuFile.setMnemonic(KeyEvent.VK_F);
        mnuFile.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuFile);

    JMenuItem mnuFileExit =new JMenuItem("Exit");
        mnuFileExit.setMnemonic(KeyEvent.VK_E);
        mnuFileExit.setDisplayedMnemonicIndex(1);
        //mnuBar.add(mnuFileExit);
        mnuFile.add(mnuFileExit);
        mnuFileExit.setActionCommand("Exit");
        mnuFileExit.addActionListener(this);

    //construct and pop the edit menu
    JMenu mnuEdit = new JMenu("Edit",true);
        mnuEdit.setMnemonic(KeyEvent.VK_E);
        mnuEdit.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuEdit);

    JMenuItem mnuEditInsert = new JMenuItem("Insert New Song");
        mnuEditInsert.setMnemonic(KeyEvent.VK_I);
        mnuEditInsert.setDisplayedMnemonicIndex(1);
        mnuEdit.add(mnuEditInsert);
        mnuEditInsert.setActionCommand("insert");
        mnuEditInsert.addActionListener(this);


        //sort menu
    JMenu mnuSort = new JMenu("Sort",true);
        mnuSort.setMnemonic(KeyEvent.VK_S);
        mnuSort.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuSort);

     //These are the items id like to sort by

   JMenuItem mnuSortArtist =new JMenuItem("by Artist");
    mnuSort.setMnemonic(KeyEvent.VK_E);
    mnuSort.setDisplayedMnemonicIndex(1);

    mnuSort.add(mnuSortArtist);
    mnuSort.setActionCommand("artist");
    mnuSort.addActionListener(this);

    JMenuItem mnuSortAlbum =new JMenuItem("by album");
        mnuSort.setMnemonic(KeyEvent.VK_M);
        mnuSort.setDisplayedMnemonicIndex(1);
        mnuSort.add(mnuSortAlbum);
        mnuSort.setActionCommand("album");
        mnuSort.addActionListener(this);

    JMenuItem mnuSortGenre = new JMenuItem("by Genre");
        mnuSort.setMnemonic(KeyEvent.VK_G);
        mnuSort.setDisplayedMnemonicIndex(1);
        mnuSort.add(mnuSortGenre);
        mnuSort.setActionCommand("genre");
        mnuSort.addActionListener(this);

    JMenuItem mnuSortHit =new JMenuItem("by Hit");
        mnuSort.setMnemonic(KeyEvent.VK_H);
        mnuSort.setDisplayedMnemonicIndex(1);
        mnuSort.add(mnuSortHit);
        mnuSort.setActionCommand("hit");
        mnuSort.addActionListener(this);

    JMenuItem mnuSortLable =new JMenuItem("by Lable");
        mnuSort.setMnemonic(KeyEvent.VK_L);
        mnuSort.setDisplayedMnemonicIndex(1);
        mnuSort.add(mnuSortLable);
        mnuSort.setActionCommand("lable");
        mnuSort.addActionListener(this);


    return mnuBar;
}//End menu contructor


//Create the conetnt pane
public Container createContentPane()
{
    //construct and populate the north panel
    JPanel northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

    //Create the JTextPane  and center Panel
    JPanel centerPanel = new JPanel();
        setTabsAndStyles(textPane);
        textPane = addTextToTextPane();
        JScrollPane scrollPane = new JScrollPane(textPane);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setPreferredSize(new Dimension(600, 300));
        centerPanel.add(scrollPane);

    //create container and set attributes
    Container c =  getContentPane();
        c.setLayout(new BorderLayout(10,10));
        c.add(northPanel,BorderLayout.NORTH);
        c.add(centerPanel,BorderLayout.CENTER);

    return c;

}//end create container method


//method to create the tab stops and set fontstyles
protected void setTabsAndStyles(JTextPane textPane)
{
    //create Tab Stops
    TabStop[] tabs = new TabStop[4];
        tabs[0] = new TabStop(100, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
        tabs[1] = new TabStop(200, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
        tabs[2] = new TabStop(300, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
        tabs[3] = new TabStop(400, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);

    //set tab style
    StyleContext tabStyle = StyleContext.getDefaultStyleContext();
    AttributeSet aset=
        tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet,tabset);
    textPane.setParagraphAttributes(aset, false);

    //set Font Style
    Style fontStyle =
        StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = textPane.addStyle("regular", fontStyle);
    StyleConstants.setFontFamily(fontStyle, "sansSerif");

    Style s = textPane .addStyle("italic",regular);
    StyleConstants.setItalic(s,true);

    s = textPane .addStyle("blod",regular);
    StyleConstants.setBold(s,true);

    s = textPane .addStyle("large",regular);
    StyleConstants.setFontSize(s,16);

}//method to ad text to textpane

public JTextPane addTextToTextPane()
{
    Document doc = textPane.getDocument();
    try
    {
        //clear previous text
        doc.remove(0, doc.getLength());

        //Insert title
        doc.insertString(0,"Artist\tAlbum\tGernre\tGreatest Hit\tRecord Lable\n",textPane.getStyle("large"));

        //insert detail
        for(int j=0;j<artist.length;j++)
        {
            doc.insertString(doc.getLength(),artist[j] + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),album[j] + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),genre[j] + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),hit[j] + "\t",textPane.getStyle("italic"));
            doc.insertString(doc.getLength(),lable[j] + "\n",textPane.getStyle("regular"));
        }//end loop
    } //end try
    catch (BadLocationException ble)
    {
        System.err.println("Couldnlt Insert Text");
    }//end catch
    return textPane;
}///end addtexttotextpane method

  public void actionPerformed(ActionEvent e)
   {
    String arg = e.getActionCommand();

   //user clicks the sort
     //   if(e.getSource() == artist)
   //   {
  /*/    switch(mnubar.getSelectedIndex())
      {
     case 0:
         sort(artist);
         break;
     case 1:
         sort(album);
         break;
     case 2:
         sort(genre);
         break;
     case 3:
         sort(hit);
         break;
     case 4:
         sort(lable);
         break;
  /*///    }//end swictch
    //    } //end if

 //user clicks exit on file menu
 if (arg.equals("Exit"))
 System.exit(0);
 //user clicks insert new dvd on edit menu
 if (arg.equals("insert"))
 {
    String newArtist = JOptionPane.showInputDialog(null, "Please enter the Artist");
    String newAlbum = JOptionPane.showInputDialog(null, "Please enter the Album for" + newArtist);
    String newGenre = JOptionPane.showInputDialog(null, "Please enter the Genre for" + newArtist);
    String newHit = JOptionPane.showInputDialog(null, "Please enter the Hit for" + newArtist);
    String newLable = JOptionPane.showInputDialog(null, "Please enter the Record Lable for " + newArtist);

     //Enlarge arrays
     artist = enlargeArray(artist);
     album = enlargeArray(album);
     genre = enlargeArray(genre);
     hit = enlargeArray(hit);
     lable = enlargeArray(lable);

     //add to arrys
     artist[artist.length-1] = newArtist;
     album[album.length-1] = newAlbum;
     genre[genre.length-1] = newGenre;
     hit[hit.length-1] = newHit;
     lable[lable.length-1] = newLable;

     //call to sort method
     sort(artist);
   //        mnuSort.setSelectedIndex(0);

 }//end if




   }//end action meth

   //Method  to enlarge an arry by 1
   public String[] enlargeArray(String[]currentArray)
   {
String[]newArray=new String [currentArray.length +1];
for(int i = 0; i<currentArray.length;i++)
    newArray[i]=currentArray[i];
return newArray;
   }//end enlarge arry method

   //method to sort arrays
   public void sort(String tempArray[])
   {
//loop to control number of passes
for(int pass = 1;pass<tempArray.length;pass++)
{
    for(int element =0 ; element<tempArray.length -1 ;element++)
        if (tempArray[element].compareTo(tempArray[element+1])>0)
        {
            swap(artist,element,element+1);
            swap(album,element, element+1);
            swap(genre,element, element+1);
            swap(hit,element, element+1);
            swap(lable,element, element+1);

        }//end if
}//end of loop
addTextToTextPane();
   }//end of sort method



  //method to swap two elements of an array
   public void swap(String swapArray[], int first, int second)
   {
String hold;//temp area to hold for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;

   } //end swap method


public static void main(String args[])
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    YourMusic f =new YourMusic();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setJMenuBar(f.createMenuBar());
    f.setContentPane(f.createContentPane());
    f.setSize(800,700);
    f.setVisible(true);
}//end main method


  }//End class
Was it helpful?

Solution 3

You should definitly take @camickr 's advice and use a class to contain this data. But it also sounds like your having trouble getting your sort method to even be called. I found a few bugs in your code


1.Your setting the properties wrong on your JMenuItems

JMenuItem mnuSortAlbum =new JMenuItem("by album");
    mnuSort.setMnemonic(KeyEvent.VK_M);
    mnuSort.setDisplayedMnemonicIndex(1);
    mnuSort.add(mnuSortAlbum);
    mnuSort.setActionCommand("album");
    mnuSort.addActionListener(this);

I think should read

JMenuItem mnuSortAlbum =new JMenuItem("by album");
    mnuSortAlbum.setMnemonic(KeyEvent.VK_M);
    mnuSortAlbum.setDisplayedMnemonicIndex(1);
    mnuSortAlbum.setActionCommand("album");
    mnuSortAlbum.addActionListener(this);
    mnuSort.add(mnuSortAlbum);


2.You actually have the sorting in actionPerformed commented out. I think this is really where your question lies. You are almost there and you just need to use the ActionCommand you placed in the variable arg.

Heres an example of how to use that action command to call the sort.

String arg = e.getActionCommand();
switch(arg) {
 case "artist":
     sort(artist);
     break;
 case "album":
     sort(album);
     break;
}

Also your spelling label as lable

OTHER TIPS

Don't keep you data in 5 separate arrays. Create a custom object (maybe called Music) which contains 5 properties (artist, album, genre, hit and label).

Then you need to create a custom Comparator to sort on whatever property you want sorted.

Here is a simple example for you to customize:

/*
**  Use the Collections API to sort a List for you.
**
**  When your class has a "natural" sort order you can implement
**  the Comparable interface.
**
**  You can use an alternate sort order when you implement
**  a Comparator for your class.
*/
import java.util.*;

public class Person implements Comparable<Person>
{
    String name;
    int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        return name + " : " + age;
    }

    /*
    **  Implement the natural order for this class
    */
    public int compareTo(Person p)
    {
        return getName().compareTo(p.getName());
    }

    static class AgeComparator implements Comparator<Person>
    {
        public int compare(Person p1, Person p2)
        {
            return p1.getAge() - p2.getAge();
        }
    }

    public static void main(String[] args)
    {
        List<Person> people = new ArrayList<Person>();
        people.add( new Person("Homer", 38) );
        people.add( new Person("Marge", 35) );
        people.add( new Person("Bart", 15) );
        people.add( new Person("Lisa", 13) );

        // Sort by natural order

        Collections.sort(people);
        System.out.println("Sort by Natural order");
        System.out.println("\t" + people);

        // Sort by reverse natural order

        Collections.sort(people, Collections.reverseOrder());
        System.out.println("Sort by reverse natural order");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by age

        Collections.sort(people, new Person.AgeComparator());
        System.out.println("Sort using Age Comparator");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by descending age

        Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
        System.out.println("Sort using Reverse Age Comparator");
        System.out.println("\t" + people);
    }
}

Well, as suggested by other's answers here is what you should aim for (there are a LOT of things worth improving):

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.*;
import javax.swing.text.*;

public class YourMusic extends JFrame implements ActionListener {

//construct components
JTextPane textPane = new JTextPane();

//initalize data in arrays
ArrayList<SongData> songs = new ArrayList<SongData> ();


//construct an instance of YourMusic
public YourMusic() {
    super("Your Music");
    songs.add(new SongData("Eminem", "Eminem Show", "Rap", "Lose Yourself", "Interscope"));
    songs.add(new SongData("Britney S", "Her Album", "Pop", "Toxic", "Britney Bith"));
    songs.add(new SongData("Dre", "Cronic", "Hip-hop", "D.R.E", "Aftermath"));
}

//create the menu system
public JMenuBar createMenuBar()
{
    ///create instance of menu bar
    JMenuBar mnuBar =new JMenuBar();
    setJMenuBar(mnuBar);

    //Construct and Populate the File menu
    JMenu mnuFile = new JMenu("File",true);
    mnuFile.setMnemonic(KeyEvent.VK_F);
    mnuFile.setDisplayedMnemonicIndex(0);
    mnuBar.add(mnuFile);

    JMenuItem mnuFileExit =new JMenuItem("Exit");
    mnuFileExit.setMnemonic(KeyEvent.VK_E);
    mnuFileExit.setDisplayedMnemonicIndex(1);
    //mnuBar.add(mnuFileExit);
    mnuFile.add(mnuFileExit);
    mnuFileExit.setActionCommand("Exit");
    mnuFileExit.addActionListener(this);

    //construct and pop the edit menu
    JMenu mnuEdit = new JMenu("Edit",true);
    mnuEdit.setMnemonic(KeyEvent.VK_E);
    mnuEdit.setDisplayedMnemonicIndex(0);
    mnuBar.add(mnuEdit);

    JMenuItem mnuEditInsert = new JMenuItem("Insert New Song");
    mnuEditInsert.setMnemonic(KeyEvent.VK_I);
    mnuEditInsert.setDisplayedMnemonicIndex(1);
    mnuEdit.add(mnuEditInsert);
    mnuEditInsert.setActionCommand("insert");
    mnuEditInsert.addActionListener(this);


    //sort menu
    JMenu mnuSort = new JMenu("Sort",true);
    mnuSort.setMnemonic(KeyEvent.VK_S);
    mnuSort.setDisplayedMnemonicIndex(0);
    mnuBar.add(mnuSort);

    //These are the items id like to sort by

    JMenuItem mnuSortArtist =new JMenuItem("by Artist");
    mnuSortArtist.setMnemonic(KeyEvent.VK_E);
    mnuSortArtist.setDisplayedMnemonicIndex(1);

    mnuSort.add(mnuSortArtist);
    mnuSortArtist.setActionCommand("artist");
    mnuSortArtist.addActionListener(this);

    JMenuItem mnuSortAlbum =new JMenuItem("by album");
    mnuSortAlbum.setMnemonic(KeyEvent.VK_M);
    mnuSortAlbum.setDisplayedMnemonicIndex(1);
    mnuSort.add(mnuSortAlbum);
    mnuSortAlbum.setActionCommand("album");
    mnuSortAlbum.addActionListener(this);

    JMenuItem mnuSortGenre = new JMenuItem("by Genre");
    mnuSortGenre.setMnemonic(KeyEvent.VK_G);
    mnuSortGenre.setDisplayedMnemonicIndex(1);
    mnuSort.add(mnuSortGenre);
    mnuSortGenre.setActionCommand("genre");
    mnuSortGenre.addActionListener(this);

    JMenuItem mnuSortHit =new JMenuItem("by Hit");
    mnuSortHit.setMnemonic(KeyEvent.VK_H);
    mnuSortHit.setDisplayedMnemonicIndex(1);
    mnuSort.add(mnuSortHit);
    mnuSortHit.setActionCommand("hit");
    mnuSortHit.addActionListener(this);

    JMenuItem mnuSortLable =new JMenuItem("by Lable");
    mnuSortLable.setMnemonic(KeyEvent.VK_L);
    mnuSortLable.setDisplayedMnemonicIndex(1);
    mnuSort.add(mnuSortLable);
    mnuSortLable.setActionCommand("lable");
    mnuSortLable.addActionListener(this);


    return mnuBar;
}//End menu contructor


//Create the conetnt pane
public Container createContentPane() {
    //construct and populate the north panel
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new FlowLayout());

    //Create the JTextPane  and center Panel
    JPanel centerPanel = new JPanel();
    setTabsAndStyles(textPane);
    textPane = addTextToTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(600, 300));
    centerPanel.add(scrollPane);

    //create container and set attributes
    Container c =  getContentPane();
    c.setLayout(new BorderLayout(10,10));
    c.add(northPanel,BorderLayout.NORTH);
    c.add(centerPanel,BorderLayout.CENTER);

    return c;

}//end create container method


//method to create the tab stops and set fontstyles
protected void setTabsAndStyles(JTextPane textPane)
{
    //create Tab Stops
    TabStop[] tabs = new TabStop[4];
    tabs[0] = new TabStop(100, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
    tabs[1] = new TabStop(200, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
    tabs[2] = new TabStop(300, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
    tabs[3] = new TabStop(400, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);

    //set tab style
    StyleContext tabStyle = StyleContext.getDefaultStyleContext();
    AttributeSet aset=
            tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet,tabset);
    textPane.setParagraphAttributes(aset, false);

    //set Font Style
    Style fontStyle =
            StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = textPane.addStyle("regular", fontStyle);
    StyleConstants.setFontFamily(fontStyle, "sansSerif");

    Style s = textPane .addStyle("italic",regular);
    StyleConstants.setItalic(s,true);

    s = textPane .addStyle("blod",regular);
    StyleConstants.setBold(s,true);

    s = textPane .addStyle("large",regular);
    StyleConstants.setFontSize(s,16);

}//method to ad text to textpane

public JTextPane addTextToTextPane()
{
    Document doc = textPane.getDocument();
    try
    {
        //clear previous text
        doc.remove(0, doc.getLength());

        //Insert title
        doc.insertString(0,"Artist\tAlbum\tGernre\tGreatest Hit\tRecord Lable\n",textPane.getStyle("large"));

        //insert detail
        for(int j=0;j<songs.size();j++)
        {
            doc.insertString(doc.getLength(),songs.get(j).getArtist() + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),songs.get(j).getAlbum() + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),songs.get(j).getGenre() + "\t",textPane.getStyle("bold"));
            doc.insertString(doc.getLength(),songs.get(j).getHit() + "\t",textPane.getStyle("italic"));
            doc.insertString(doc.getLength(),songs.get(j).getLable() + "\n",textPane.getStyle("regular"));
        }//end loop
    } //end try
    catch (BadLocationException ble)
    {
        System.err.println("Couldnlt Insert Text");
    }//end catch
    return textPane;
}// end addtexttotextpane method

public void actionPerformed(ActionEvent e) {
    String arg = e.getActionCommand();
    System.out.println(arg);
    if (arg.equals("artist") || arg.equals("album") || arg.equals("genre") || arg.equals("hit") || arg.equals("lable")) {
        this.sort(arg);
    }
    //user clicks exit on file menu
    else if (arg.equals("Exit"))
        System.exit(0);
    //user clicks insert new dvd on edit menu
    else if (arg.equals("insert")) {
        String newArtist = JOptionPane.showInputDialog(null, "Please enter the Artist");
        String newAlbum = JOptionPane.showInputDialog(null, "Please enter the Album for " + newArtist);
        String newGenre = JOptionPane.showInputDialog(null, "Please enter the Genre for " + newArtist);
        String newHit = JOptionPane.showInputDialog(null, "Please enter the Hit for " + newArtist);
        String newLable = JOptionPane.showInputDialog(null, "Please enter the Record Lable for " + newArtist);

        //Enlarge arrays
        songs.add(new SongData(newArtist, newAlbum, newGenre, newHit, newLable));
    }//end if
}//end action meth


//method to sort arrays
public void sort(final String criteria) {
    Collections.sort(songs, new Comparator<SongData> () {
        @Override
        public int compare(SongData song1, SongData song2) {
            switch (criteria) {
            case "artist":  return song1.getArtist().compareTo(song2.getArtist());
            case "album":   return song1.getAlbum().compareTo(song2.getAlbum());
            case "genre":   return song1.getGenre().compareTo(song2.getGenre());
            case "hit":     return song1.getHit().compareTo(song2.getHit());
            case "lable":   return song1.getLable().compareTo(song2.getLable());
            default:        return 0;
            }
        }
    });
    addTextToTextPane();
}//end of sort method


public static void main(String args[]) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    YourMusic f =new YourMusic();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setJMenuBar(f.createMenuBar());
    f.setContentPane(f.createContentPane());
    f.setSize(800,700);
    f.setVisible(true);
}//end main method


}//End class

Somewhat better, now, as far as SongData goes, I've kept it simple (getters and setters generated by eclipse and nothing else):

public class SongData {

private String artist, album, genre, hit, lable;

public SongData(String artist, String album, String genre, String hit, String lable) {
    setArtist(artist);
    setAlbum(album);
    setGenre(genre);
    setHit(hit);
    setLable(lable);
}

public String getArtist() {
    return artist;
}

public void setArtist(String artist) {
    this.artist = artist;
}

public String getAlbum() {
    return album;
}

public void setAlbum(String album) {
    this.album = album;
}

public String getGenre() {
    return genre;
}

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

public String getHit() {
    return hit;
}

public void setHit(String hit) {
    this.hit = hit;
}

public String getLable() {
    return lable;
}

public void setLable(String lable) {
    this.lable = lable;
}

public String toString() {
    String data = "";
    data = getArtist() + ", " + getAlbum() + ", " + getGenre() + ", " + getHit() + ", " + getLable();
    return data;
}

}

This works, but again, a LOT of fixes should be made, regarding null values, switch cases... But instead of dealing with multiple arrays you now have a list with a special object that's fairly easy to sort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top