Results 1 to 3 of 3
Thread: Help with sorting two types:
- 02-11-2010, 04:18 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 6
- Rep Power
- 0
Help with sorting two types:
Need help sorting a string and a double.
Here is current code:
First part:
Second PartJava 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; } }
Any suggestions would be great!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]); } }
-
What are you hoping to achieve with this?
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.Java Code:public void Item(String listItem, double price) { }
Much better would be
This may seem like nitpicking, but these details are very important.Java Code:public Item(String listItem, double price) { this.listItem = listItem; this.price = price; }
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 04:34 PM.
- 02-11-2010, 04:37 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
-
Sorting/Searching Objects with multiple types.
By gcampton in forum New To JavaReplies: 20Last Post: 10-21-2009, 11:58 PM -
again -.- unsigned types
By prich in forum NetworkingReplies: 1Last Post: 10-17-2009, 05:53 PM -
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
Collection Types
By DavidG24 in forum New To JavaReplies: 2Last Post: 04-18-2009, 05:03 PM -
WHy does it say imcompatable types?
By PeterFeng in forum New To JavaReplies: 3Last Post: 01-14-2009, 05:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks