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