Results 1 to 5 of 5
  1. #1
    Faye Rett's Avatar
    Faye Rett is offline Member
    Join Date
    Mar 2010
    Location
    USA
    Posts
    3
    Rep Power
    0

    Question Sorting Two Arrays

    Hello everyone! I read a few of these posts and you all seemed like a very beginner friendly lot. So here goes.

    I am trying to create a program that sorts out two different arrays (items and prices). Now, I want the user to have the option on having it sorted cheapest to most expensive or alphabetically by the names. But I want to keep the items and the prices together, if that makes sense. I've read a few things on Arrays.sort(), but the information just stopped after an example of how it could sort one array. I haven't written out any code yet. But I do have some guidelines for what I need to understand. Can anyone show me an example or even give me a link that has more information on sorting two arrays together based on one array's criteria.

    NOTE: Yes, I know there are lots of hunks of code missing. I know what I want to do there, just not how to do it. There also might be some problems with my variable names. I tried to find them all, but I've been staring at the code for at leas six hours straight...you know how it can get.

    This is what I have as my driver class.

    Java Code:
    import java.util.Arrays
    import javax.swing.JOptionPane;
    public class CoffeeDriver
    {
    	public static void main (String[] args)
    	{
    		String userInput;
    		int x;
    		int selectSort;
    		String[] someItem = new String[5];
    		double[] somePrice = new double[5];
    		//Just incase, I always have this final int until I'm ready to complete the program.
    		final int PLACE_HOLDER = 6;
    		JOptionPane.showMessageDialog(null, "Welcome to the Coffee Shop!");
    		userInput = JOptionPane.showInputDialog(null, "Please indicate how you would like to sort the menu.  To sort by price, press 1.  To sort by name, press 2.  To exit the program, press 3.");
    		selectSort = Integer.parseInt(userInput);
    		if (selectSort < 1 || selectSort > 3);
    			JOptionPane.showMessageDialog(null, "Please enter a valid selection.");
    		//Not finished with this yet
    		if (selectSort == 1);
    			sortPrice();
    		//Still not complete here.
    		if (selectSort == 2);
    			sortName();		
    		//given option to end program
    		if (selectSort == 3);
    			JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop.  Have a great day!");
    	}
    	public static double sortPrice();
    	{
    	//will have my double sorted for my Price Array here
    	}
    	public static String sortName();
    	{
    	//Will have my string sorted for my Name Array here
    	}
    }
    Below is the get/sets and the constructor.
    Java Code:
    public class item
    {
    	private String itemName;
    	private double itemPrice;
    	public String getName()
    	{
    		return itemName;
    	}
    	public double getPrice;
    	{
    		return itemPrice;
    	}
    	public item (String itemName, double itemPrice)
    	{
    		double[] ItemPrice = {0.0, 1.00, 2.00, 1.50, 1.25, 0.75};
    		String[] ItemName = {"Item", "Coffee", "Water", "Milk", "Bagel", "Donut"};
    	}
    	public void setItemName(String someItem)
    	{
    		itemName = someItem;
    	}
    	public void setItemPrice(double somePrice)
    	{
    		itemPrice = somePrice;
    	}
    }
    Last edited by Faye Rett; 03-07-2010 at 12:16 AM.

  2. #2
    Faye Rett's Avatar
    Faye Rett is offline Member
    Join Date
    Mar 2010
    Location
    USA
    Posts
    3
    Rep Power
    0

    Default

    Anyone? Please?

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

    Default

    Can anyone show me an example or even give me a link that has more information on sorting two arrays together based on one array's criteria.
    No. My advice is don't do it that way.

    Use an OO approach to do it properly. Something like my example in this posting.

  4. #4
    Faye Rett's Avatar
    Faye Rett is offline Member
    Join Date
    Mar 2010
    Location
    USA
    Posts
    3
    Rep Power
    0

    Default

    Wow...there is a lot of stuff in there that I have never seen in my book before. But this is for a rather important assignment, so I have to use certain things. I know, asking for homework help is a horrible thing but I'm not looking for anyone to put this together for me; promise.

    This is what I have to have for the project for a perfect grade. As you can see from the code I put up there, I'm missing some key elements.

    First class name Item:

    Data Fields are 2 only and remember these data fields should be private.

    String itemName;
    double itemPrice;

    The public methods for this class will be the following:

    A constructor the takes a String and a double as the input arguments to initialize the data fields.

    getItemName
    setItemName
    getItemPrice
    setItemPrice

    Now on to the second class to be called CoffeeDriver which will contain the main method.

    Methods required for the Driver class:

    main - will create an array of Item Objects using the data above to set each item’s information. This information will NOT come from the user of the project. In addition, main should request from the user which order they wish to see the data items list, by price or by item name. Use a JOptionpane for this request. You might want to include a question to ask the user if they are finished with the program??

    sortName this method will sort the array of item objects by the item name. Remember when creating your sort that when you are comparing String values you cannot use ==. You will want to use the compareTo() String method. See note below. You must also keep in mind that you will compare on the String of the item name only, but when you need to swap for the sort, you will be swapping a complete object which includes the name and price, not just the itemNames. This method will display the array of objects in item name order. The display will include both the itemName and the itemPrice.

    sortPrice this method will sort the array of item objects by price. Again remember, when you need to swap the prices, you are swapping the entire object and not just the prices. Again this method will display the array of objects in price order. The display will include both the itemName and the itemPrice.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    So your sortName() will use getName() and your sortPrice() will use getPrice(). For you to use these you are going to need a sorting algoritham. There are many on wikipedia: List of algorithms - Wikipedia, the free encyclopedia

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 03:18 PM
  3. Replies: 0
    Last Post: 01-17-2010, 07:09 AM
  4. Sorting Arrays by enum
    By sahity1a@yahoo.com in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 10:08 AM
  5. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 11:30 AM

Posting Permissions

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