Results 1 to 3 of 3
  1. #1
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Help with sorting two types:

    Need help sorting a string and a double.
    Here is current code:

    First part:
    Java Code:
    import javax.swing.*;
    public class Item
    {
            final int NUM_ITEMS = 5;
            String listItem;
            double price;
    
          
            public void Item(String listItem, double price)
            {
            }
    
          
            public String getListItem()
            {
                    return listItem;
            }
            public double getPrice()
            {
                    return price;   
            }
            public void setListItem(String oneListItem)
            {
                    listItem = oneListItem;
            }       
            public void setPrice(double onePrice)
            {
                    price = onePrice;
            }
    }
    Second Part
    Java Code:
    import javax.swing.*;
    import java.util.*;
    public class driver
    {
            public static void main(String[]args)
            {
                    char choice;
                    String[] listItem = {"Coffee", "Cappuccino", "Hot Chocolate", "Bagel", "Donut"};
                    double[] price =        {1.00,   2.00,     1.50,                         1.25,     .75};
                    
                    choice = JOptionPane.showInputDialog(null,"To view menu by item name, enter n \n To view by price enter p",
                            "Menu Viewer", JOptionPane.QUESTION_MESSAGE); 
    
                    if(choice.equalsIgnoreCase("n")||choice.equalsIgnoreCase("p"))
                            {
                            switch(choice)
                                    {
                                    case 'n':  
                                            sortName (listItem, price);
                                            break;
                                    case 'p': 
                                            sortPrice(listItem,price);
                                            break;
                                    default:
                                            JOptionPane.showMessageDialog(null,"Invalid entry, Please try again");
                                            break;
                                    }
                            }
            
                             
            }
            
            public static void sortPrice(String[] listItem, double[] price)
            {
                    Arrays.sort(price);     
                    for(int count=0;count<=4;++count)
                            System.out.println(listItem[count]+"  "+price[count]);
            }
    
            public static void sortName(String[] listItem,double[] price)
            {
                    Arrays.sort(listItem);
                    
                    for(int count=0;count<=4; ++count)
                     System.out.println(listItem[count]+"  "+ price[count]);
            }
    
    }
    Any suggestions would be great!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    29

    Default

    What are you hoping to achieve with this?
    Java Code:
            public void Item(String listItem, double price)
            {
            }
    You know that this is not a constructor since it has a void return type, and even if it were, all it would do would be prevent you from using a default constructor and only allow use of a non-functioning parameter-requiring constructor.

    Much better would be
    Java Code:
            public Item(String listItem, double price)
            {
               this.listItem = listItem;
               this.price = price;
            }
    This may seem like nitpicking, but these details are very important.

    Next, if you are going to do sorting, look at having Item implement a Comparable<Item> interface.

    edit: next, in your driver class (it should be Driver, by the way), where do you ever use your Item class???
    Last edited by Fubarable; 02-11-2010 at 05:34 PM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    10

    Default

    You need to be able to sort the Item object. For this you need to understand the Compare interface and how Comparators work.

    Check out this posting for a simple example.

    When you understand the above solution, for a more complex solution but also a reusable solution you can also check out a Bean Comparator.

Similar Threads

  1. Sorting/Searching Objects with multiple types.
    By gcampton in forum New To Java
    Replies: 20
    Last Post: 10-22-2009, 12:58 AM
  2. again -.- unsigned types
    By prich in forum Networking
    Replies: 1
    Last Post: 10-17-2009, 06:53 PM
  3. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 11:29 PM
  4. Collection Types
    By DavidG24 in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 06:03 PM
  5. WHy does it say imcompatable types?
    By PeterFeng in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 06:54 PM

Posting Permissions

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