I am getting an error message that is quite new to me. I normally can call methods without incident but this time, I saw a new error message. This code is sample coding I am using to see how to fix three of my issues.

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

Second Class

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


 
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 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 void sortPrice(Items array[]) 

{
//  sorts the array by price and displays the list
int x;
 Items temp; 
 for (x = 0; x < array.length; x++)
  { if(array[x].getPrice() > array[x+1].getPrice())
   { temp = array[x]; array[x] = array[x+1]; array[x+1] = temp; }
	 JOptionPane.showMessageDialog(null, array[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);
		 if (selectSort < 1 || selectSort > 4);
		  JOptionPane.showMessageDialog(null, "Please enter a valid selection.");
		  if (selectSort == 1);
		 //error  sortPrice(arr);
		 
		   if (selectSort == 2);
	// error sortName(arr);
		    if (selectSort == 3);
		   JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!"); 

				 
}
}

Also, I have never sorted an array like this before, so if anyone can point me in the right direction, I would be glad to learn.

Thanks

Nobody is going to try to check this code until you indent it properly - it's just too hard to match up the {}.
And please tell us what the exact error message is, this isn't a quiz.
And class Items should be called Item - each instance represents exactlyy one item, not multiple items.

Here is the error message

non-static method sortPrice(Items[]) cannot be referenced from a static context

I thought I provide the message but I guess I didn't. And again, any links to properly sorting this array by ItemName and ItemPrice would be appreciated.

Thanks,

The message says it all. sortPrice is an instance (AKA non-static) method that needs a single instance of the class to execute again. You're calling it from your static main method, so there is no instance of the class.
The quick fix is to declare all the methods in CoffeeDriver static, but personally I think the sort methods belong in the Item class not in the test driver. I would consider moving them to Item, make them static, and call them as Item.sortPrice(...) etc. OR make a class to represent a collection of Items with methods to add Items to the collection, and sort it in different sequences. The last solution would probably be best in a real-life application becuase it will cope with far more of the complexity and variations that plague real life.

Thanks that fixed the problem there but when I run the app. I get


Items@1a7bf11
Items@6e1408
Items@10ef90c
etc..

I am not sure why my output is appearing this way..Any advice

When you try to display one of your Items, Java uses the Item's toString() method to get a displayable version. You are getting the toString() inherited from Object, which just gives the name of the class and an incomprehensible object reference.
Define your own public String toString() method in the Items class to return a String that makes sense to you (eg "Item: " + name + " price:" + price) and Java will use that to display the Item.

Okay, I have added that to the Item class but how do I use it in the CoffeeDriver class.
If you have a link, that would be helpful so I can fully understand this method.

Thanks

Oh, I see I just reread and understood what you were saying. Thanks. It finally is displaying yet it doesn't read the entire Array.

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.