0
public class BookStore {
    
        /**************************************************
    Kim works in the school bookstore, selling pens, notebooks, etc.
    They have prices written in a notebook.  When prices change 
    they must cross out old prices - that's messy.  They want
    a computer program that stores and displays prices.
    Item names and prices are stored in PARALLEL ARRAYS.
     ***************************************************/
    
        String[] items = {
            "pencil", "pen", "sharpie", "notebook A", "notebook B",
                "eraser", "tippex", "usb stick", "glue", "tape"
        };
    
        float[] prices = {
            0.75f, 1.50f, 1.20f, 1.90f, 2.50f,
            0.50f, 1.75f, 5.00f, 1.25f, 2.00f
        };
        public static void main(String[] args) {
            String name = "";
            do {
                name = input("Type the name of an item");
                float price;
                price = getPrice(name);
                if (price > 0) {
                    output(name + " = " + price);
                } else {
                    output(name + " was not found");
                }
            } while (name.length() > 0); // type nothing to quit
        }
        
        float getPrice(String name) // search for name, return price
        { // the price is not case-sensitive
            for (int x = 0; x < prices.length; x = x + 1) {
                if (items[x].equalsIgnoreCase(name)) // not cases-sensitive
                {
                    return prices[x];
                }
            }
            return 0.00f;
        }
    
        static public String input(String prompt) {
            return javax.swing.JOptionPane.showInputDialog(null, prompt);
        }
    
        static public void output(String message) {
            javax.swing.JOptionPane.showMessageDialog(null, message);
        }
    }

So I run this code and I get the following error:

run:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code

  • non-static method getPrice(java.lang.String) cannot be referenced from a static context at BookStore.main(BookStore.java:26)

Java Result: 1

BUILD SUCCESSFUL (total time: 2 seconds)

Which is confusing because I tried to just tack on the 'static' modifier in front of getPrice() but that creates a whole host of other errors. Maybe one of you guys can assist me..? Any help is appreciated.

Community
  • 1
  • 1
blue sky
  • 161
  • 1
  • 13
  • 2
    getPrices () is an instance method and should be, since prices and items that are accessed inside it are instance properties, the are property of one particular BookStore instance, not the whole class of BookStores. Instantiate a BookStore instance in your main: `BookStore b = new BookStore ()`and you can call `b.getPrices ()` – user2390182 Nov 28 '15 at 07:26
  • 2
    And also [this](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – MadProgrammer Nov 28 '15 at 07:30

1 Answers1

0

Like this:

public static void main(String[] args) {
        BookStore bs = new BookStore();

        String name = "";
        do {
            name = bs.input("Type the name of an item");
            float price;
            price = bs.getPrice(name);
            if (price > 0) {
                output(name + " = " + price);
            } else {
                output(name + " was not found");
            }
        } while (name.length() > 0); // type nothing to quit
    }
KostasC
  • 1,076
  • 6
  • 20
  • 40