For my program I had to create two classes one named and another one called I need to display and sort the following:
Item Name Price
Coffee $1.00
Water $2.00
Milk $1.50
Bagel $1.25
Donut $0.75

I have the class and array created to display this but I am currently have trouble with sorting this array. Any help will be greatly appreciated.

Heres my code:


//Class named Item
public class Item {

private String name;   //String instance to hold name
private double price;  //Double instance to hold price

//Constructor to return values
public Item(String itemName, double itemPrice)
{ = itemName;
    this.price = itemPrice;

//Get method for name
public String getName()



//Set method for name
public void setName(String Itemname)
{ = Itemname;


//Get method for price
public double getPrice()

    return this.price;


//Set method for price
public void setPrice(double Itemprice)

    this.price = Itemprice;



import java.util.Scanner;  //Needed for user input

//Class named CoffeeDriver
public class CoffeeDriver {

//Default main class
public static void main(String[] args) {

    String ans;
    Scanner keyBoard = new Scanner(; //Allows user input

    //Array to hold all of the items and prices
    Item Item[] = new Item[5];

        Item[0] = new Item("Coffee", 1.00);

        Item[1] = new Item("Water ", 2.00);
        Item[1].setName("Water ");

        Item[2] = new Item("Milk  ",   1.50);
        Item[1].setName("Milk  ");
        Item[3] = new Item("Bagel ",  2.00);
        Item[1].setName("Bagel ");
        Item[4] = new Item("Donut ",  0.75);
        Item[1].setName("Donut ");

    //Welcome message and directions
    System.out.println("Welcome to Wings Coffee Shop! \n" 
            + "Heres a unsorted list of our menu: ");
            System.out.println("Item Name" + "        Price");

    //For loop to print the unsorted values of the array
    for (int i = 0; i < Item.length; ++i)
        System.out.print( Item[i].getName());
        System.out.print( "           ");

    //Statement to ask the use r how they would like to sort
    System.out.println ("Would you like to see these items sorted by "
            + "name or price?  (n/p): ");
    ans = keyBoard.nextLine(); //Input from user on which sort to view

    for (int i = 0; i < Item.length; ++i)
    sortPrice(Item[i].getPrice(), Item.length);



//Method to sort by price
//public static void sortPrice(double Item[] )

   public static void sortPrice(double item[], int size){    
      double temp;

 for(int i=0;i<size;i++)
     for(int j=0;j<size-i-1;j++)
            temp = item[j];


The error that I am recieving is in the call to my sort function in my main method. it is saying double cannot be converted to double[].

MODERATOR: code block fixed: indent total block of your code one extra tab by selecting it after paste and clicking TAB key

That is true. You can not convert a double to an array of doubles. You must change one or the other so that they are both the same type: double or double[]

Please post the full text of the error message. Your editted version leaves off information.

I guess that's what I am having trouble with. I am not sure on how to convert them all to the same type. My full error code reads

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: sortPrice
Java Result: 1

When I hover over the error it reads:

method sortPrice in class cannot be applied to given types;
required: double[],int
found: double,int
reason: actual argument double cannot be converted to double[] by method invocation conversion

The method requires an array of double. You are trying to pass it a single double value.
Why are you calling the method? What do you want the method to do for you? If you put the one double variable in an array will the method do what you want? I don't think sorting an array with one thing would be of any value.

Item Item[] = new Item[5];
You also need to fix this. You dont even have a variable. You cannot use a class name as a variable.

EDIT: Apparently you can :D but I have never seen it before nor do I advise to do it, unless of course there is a logical reason for it

Your correct is saying that is weird code. Variable names should start with lowercase letters.

Yeah I guess I wasn't really thinking when i wrote that. I changed it to
Item menu[] = new Item[5];
But i' still pretty lost with the call to the sort method.

What do you want to be sorted by the method? Why call it? Remove the call. It useless to sort one item.

seems to me that this would work if in your sortPrice method, you switched:



if(item[j].getPrice() > item[j+1].getPrice()

Then, instead of using your loop to send:

sortPrice(Item[i].getPrice(), Item.length);

you'll use:

sortPrice(Item, Item.length); //you probably ought to rename Item

Also, the signature for sortPrice should be changed to:

public static void sortPrice(Item[] item, int size)

And actually, you could leave off int size, and simply use i < item.length in the for loop in the sortPrice method.

As it stands, you're sending a single double to be sorted 5 times, when you want to send an array of Items that contains 5 double values to be sorted once. (At least I think that's what you want to do)