I am losing a value in the array with my bubble sorting and the sorting does nothing.
Here is the Items Class

public class Items 
{ 
// * A String instance variable to hold the item name 
private String itemName; 
// * A double instance variable to hold the price 
private double itemPrice; 


// * A constructor that takes a String and double to initialize the instance variables 

// public Items () 
// { 
// //used for sorting
// } 
public Items (String itemName, double itemPrice) 
{ 
this.itemPrice = itemPrice; 
this.itemName = itemName; 
} 
// * A get and set method for each instance variable 

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;
} 
}

Here is the Class that creates the array and sorts the array by price and by name

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


// Once you have this class created, you write a second class named CoffeeDriver.   This class has the following methods: 
public class CoffeeDriver
{

// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen 
public static void sortName(Items arr[])
{
// sorts array by name and displays the items and prices
 int x;
  Items temp; 
  for (x = 0; x < arr.length; x ++)
   
    { if(arr[x].getName().compareTo(arr[x+1].getName()) > 0)
  { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }
 
	JOptionPane.showMessageDialog(null,(arr[x])); 
                   }
			}
	 
	 

 


 
// sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen 
public static void sortPrice(Items arr[]) 

{
//  sorts the array by price and displays the list
int x;
 Items temp; 
 for (x = 0; x < arr.length; x++)

  { if(arr[x].getPrice() > arr[x+1].getPrice())
    { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }
	 JOptionPane.showMessageDialog(null, arr[x]);
	 

	 } 
	 
}
// main - It creates an array of Item objects using the data above to set each Item's information. 

public static void main(String []args)
{

String userSorted; 
int x;
 
  Items arr[] = new Items[5];
   arr[0] = new Items("Coffee", 1.00); 
	arr[1] = new Items("Water", 2.00);
	 arr[2] = new Items("Milk", 1.50);
	  arr[3] = new Items("Bagel", 1.25);
	   arr[4] = new Items("Donut", 0.75);

		int selectSort;
		JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
		userSorted = 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(userSorted);
	// insert do while loop here
	
		  if (selectSort == 1);
		   sortPrice(arr);
		 
		   if (selectSort == 2);
	       sortName(arr);
		    if (selectSort == 3);
		   JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!"); 
System.exit(0);
				 
}
}

I know the brackets are off a bit but any help with the sorting would be appreciated.

Thanks

You need to look again at the algorithm for bubble sorting - it needs two nested loops and you've only got one. Also arr[x+1] is going to fall off the end of the array with your current loop.

ps: "I know the brackets are off a bit" - that's inexcusable. You will never be able to debug a program properly without being able to see which } goes with which {. If you haven't already, download a proper programmer's editor that will do it for you, or use a proper IDE such as Eclipse or Netbeans

You need to look again at the algorithm for bubble sorting - it needs two nested loops and you've only got one. Also arr[x+1] is going to fall off the end of the array with your current loop

Is there an example of bubble sorting that would apply to what I am trying to do? I am still running into problems and can't find out what is going wrong.

Google.

Well thanks anyway. I tried Google and the examples I got are already in my code. So that doesn't help. If you have anything further to add I would appreciate it.

Thanks

I Googled "java bubble sort"
The third hit was http://www.informit.com/articles/article.aspx?p=31526&seqNum=2
This includes the following source code

public void bubbleSort()
   {
   int out, in;

   for(out=nElems-1; out>1; out--)  // outer loop (backward)
     for(in=0; in<out; in++)    // inner loop (forward)
      if( a[in] > a[in+1] )    // out of order?
        swap(in, in+1);     // swap them
   } // end bubbleSort()

Which, as you can see, has two nested loops where you just have one loop

I tried that one. I got an error message say cant use comparative operator.
Thanks anyway.

I didn't expect that code to work unchanged in your app - but it does give you the correct algorithm and loop structures for you to tailor to your own code. Check out the error message carefully - most Java error messages are very good at telling you exactly what you need to know.

I have changed the code and got it to work. I only have to add the format to currency if I can. Any insight into that would be helpful otherwise I will post this as solved.
Here is what I have

import java.util.*;
 public class Items 
{ 
// * A String instance variable to hold the item name 
private String itemName; 
// * A double instance variable to hold the price 
private double itemPrice; 


// * A constructor that takes a String and double to initialize the instance variables 

public Items (String itemName, double itemPrice) 
{ 
this.itemPrice = itemPrice; 
this.itemName = itemName; 
} 
public Items()
{
}
// * A get and set method for each instance variable 

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());
        }
    };
	 }

Tester Class

import javax.swing.*;
import java.util.*; 
import java.text.*;//Imported for NumberFormat

// Once you have this class created, you write a second class named CoffeeDriver.   This class has the following methods: 
public class CoffeeDriver
{

// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen 
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);    }

//  sorts the array by price and displays the list
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);  
	 }
// main - It creates an array of Item objects using the data above to set each Item's information. 

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, "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(userSorted);
	// insert do while loop here
	
		  if (selectSort == 1)
 		   sortPrice(arr);
		 	  if (selectSort == 2)
				 sortName(arr);
		   	 if (selectSort == 3)
		         JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!"); 
System.exit(0);
				 
}
}

Have a look at NumberFormat and its getCurrencyInstance() method ;-)

I have been looking into the NumberFormat and it's getCurrencyInstance() but I get an error

Cannot format given Object as a Number

Here is the line of code causing this error.

All this is new to me, but I am still searching for a way to get this to work.

NumberFormat money = NumberFormat.getCurrencyInstance();


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

        JOptionPane.showMessageDialog(null, money.format(out));    }

out should contain a single number, not a list of numbers with \n between them!
Format each number separately, then concatenate all the formatted versions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.