Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help figuring out this code

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help figuring out this code

    Where do I change the code so that on the coffee driver file the compiler recognizes the n and p on:

    if (selectSort == n)
    sortPrice(arr);
    if (selectSort == p)
    sortName(arr);

    Here are my two files Items and Coffee driver




    import java.util.*;
    public class Items
    {

    private String itemName;

    private double itemPrice;

    public Items (String itemName, double itemPrice)
    {
    this.itemPrice = itemPrice;
    this.itemName = itemName;
    }
    public Items()
    {
    }
    public String getName()
    {
    return itemName;
    }
    public double getPrice()
    {
    return itemPrice;
    }
    public void setItemName(String someItem)
    {
    itemName = someItem;
    }
    public void setItemPrice(double somePrice)
    {
    itemPrice = somePrice;
    }
    public String toString()
    {
    return "Item: " + itemName + " price:" + itemPrice;
    } static final Comparator<Items> NAME_ORDER = new Comparator<Items>() {
    public int compare(Items e1, Items e2) {
    return e1.getName().compareTo(e2.getName());
    }
    };
    static final Comparator<Items> PRICE_ORDER = new Comparator<Items>() {
    public int compare(Items e1, Items e2) {
    return Double.compare(e1.getPrice(), e2.getPrice());
    }
    };
    }






    import javax.swing.*;
    import java.util.*;
    import java.text.*;

    public class CoffeeDriver
    {

    public static void sortName(Items arr[]) {

    Arrays.sort(arr, Items.NAME_ORDER);
    String out="";
    for(Items item : arr){
    out+= item.toString()+"\n";
    }
    JOptionPane.showMessageDialog(null, out); }

    public static void sortPrice(Items arr[]){

    Arrays.sort(arr, Items.PRICE_ORDER);
    String out="";
    for(Items item : arr){
    out+= item.toString()+"\n";
    }
    JOptionPane.showMessageDialog(null, out);
    }

    public static void main(String []args)
    {
    String userSorted;
    int x;

    Items arr[] = new Items[5];
    arr[0] = new Items();
    arr[0].setItemName("Coffee");
    arr[0].setItemPrice(1.00);
    arr[1] = new Items();
    arr[1].setItemName("Water");
    arr[1].setItemPrice(2.00);
    arr[2] = new Items();
    arr[2].setItemName("Milk");
    arr[2].setItemPrice(1.50);
    arr[3] = new Items();
    arr[3].setItemName("Bagel");
    arr[3].setItemPrice(1.25);
    arr[4] = new Items();
    arr[4].setItemName("Donut");
    arr[4].setItemPrice(0.75);

    int selectSort;
    JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
    userSorted = JOptionPane.showInputDialog(null, "We have a great list items on our menu. Would you like to see these items sorted by name or by price? (n / p)");
    selectSort = Integer.parseInt(userSorted);


    if (selectSort == n)
    sortPrice(arr);
    if (selectSort == p)
    sortName(arr);

    System.exit(0);


  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help figuring out this code

    i am looking at it now

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out this code

    thank you

  4. #4
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help figuring out this code

    first thing the n and p variables are never been declared

    --- Update ---

    do you want a manual input to switch the sorting?

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out this code

    yes

  6. #6
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help figuring out this code

    so you want this textbased?

    --- Update ---

    because that is easy to do

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring out this code

    yes that is fine

  8. #8
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help figuring out this code

    ok then i am going to implement the scanner

    --- Update ---

    import javax.swing.*;
    import java.util.*;
    import java.text.*;
     
    public class CoffeeDriver{
     
    	//The scanner
    	public static Scanner scanner = new Scanner(System.in);
     
    	//string needed for the scanner
    	public static String userinput;
     
    	public static void sortName(Items arr[]) {
     
    		Arrays.sort(arr, Items.NAME_ORDER);
    		String out="";
    		for(Items item : arr){
    			out+= item.toString()+"\n";
    		}
    		JOptionPane.showMessageDialog(null, out); 
    		}
     
    	public static void sortPrice(Items arr[]){
     
    		Arrays.sort(arr, Items.PRICE_ORDER);
    		String out="";
    		for(Items item : arr){
    			out+= item.toString()+"\n";
    		}
    		JOptionPane.showMessageDialog(null, out);
    	}
     
    	public static void main(String []args)	{
    		String userSorted;
    		int x;
     
    		Items arr[] = new Items[5];
    		arr[0] = new Items();
    		arr[0].setItemName("Coffee");
    		arr[0].setItemPrice(1.00);
    		arr[1] = new Items();
    		arr[1].setItemName("Water");
    		arr[1].setItemPrice(2.00);
    		arr[2] = new Items();
    		arr[2].setItemName("Milk");
    		arr[2].setItemPrice(1.50);
    		arr[3] = new Items();
    		arr[3].setItemName("Bagel");
    		arr[3].setItemPrice(1.25);
    		arr[4] = new Items();
    		arr[4].setItemName("Donut");
    		arr[4].setItemPrice(0.75);
     
    		int selectSort;
    		JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
    		userSorted = JOptionPane.showInputDialog(null, "We have a great list items on our menu. Would you like to see these items sorted by name or by price? (n / p)");
    		selectSort = Integer.parseInt(userSorted);
     
    		//dialog for the decision making
    		System.out.println("What should these be sorted by?");
    		System.out.println("Name or price?");
    		System.out.println("Type N for sorting on name.");
    		System.out.println("Type P for sorting on price");
     
    		//user input
    		userinput = scanner.next();
     
    		//input compared to the correct answer
    		if (userinput.equals("P"))
    			sortPrice(arr);
    		if (userinput.equals("n"))
    			sortName(arr);
     
    		System.exit(0);
    	}
    }

    This should do it i guess.
    But wenn i test it it works fine and it trys to enter the sortPrice(arr); or sortName(arr); and then nothing happens because it is terminated.
    What do you want to do with that?

    --- Update ---

    i have put commands where i changed stuff around. I only declared some variables and the stuf where it gets sorted]

    --- Update ---

    i have put commands where i changed stuff around. I only declared some variables and the stuf where it gets sorted]

    --- Update ---

    nevermind it works fine

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help figuring out this code

    @The_pizzaguy The idea here on this forum is to help students learn how to program.
    It is not to do the coding for them.

    See: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help figuring out this code

    srry norm i just like to help

    --- Update ---

    @ sfsb6 this is a way to choose between a few options with the scanner class in a textbased programm.
    If you want to use it in a GUI then you should action/eventlisteners.

Similar Threads

  1. need some help figuring out what some code does
    By needshelp in forum Other Programming Languages
    Replies: 2
    Last Post: December 9th, 2012, 05:38 PM
  2. Having trouble figuring out how to recursively do this.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 17
    Last Post: May 1st, 2012, 09:50 PM
  3. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  4. Need help figuring out the problem (GUI)
    By Prescott in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 8th, 2011, 12:01 AM
  5. Figuring Out Object Arrays
    By bengregg in forum Collections and Generics
    Replies: 2
    Last Post: April 5th, 2011, 05:55 AM