Question

I know these must be basic errors, but I'm not sure how to fix them.

I changed my class name to Interface & now Java has a problem with it.

Also, in my switch statement, I've tried to call the enterData method, but I'm getting an error on this line as well as on this line... "private static void enterData()" <-- it says a "token" is missing on this line?

I'm trying to call a method from case 0, but it isn't working.

import java.util.Scanner;

    public class Interface { 
        private void run() 
        {

            Scanner console = new Scanner(System.in);
            Store store1 = new Store(); // MUST DO THIS

            int demandRate, option, end;
            double setupCost, unitCost, inventoryCost;
            double sellingPrice, optimalOrder;
            String name;

            do {
                System.out.println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
                option = console.nextInt();
                switch(option)
                {
                case 0: enterData();
                        break;
                case 1:
                        break;
                case 2:
                        break;
                case 9: System.out.println("You chose to exit the program.");
                        break;
                default: System.out.println("Please choose a valid option.");
                }
            } while (option != 9);

            private static void enterData()
            {
                System.out.println("Product name between 3 & 10 characters long: ");
                name = console.nextLine();
                while ((name.length() < 3) || (name.length() > 10)) {
                    System.out.println("Please put in a name between 3 & 10 characters long.");
                    name = console.nextLine();
                }           
                name = name.toLowerCase();

                System.out.println("Demand rate: ");
                demandRate = console.nextInt();
                while (demandRate <= 0) {
                    System.out.println("Please put in a positive integer.");
                    demandRate = console.nextInt();
                }

                System.out.println("Setup cost: ");
                setupCost = console.nextDouble();
                while (setupCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    setupCost = console.nextInt();
                }

                System.out.println("Unit cost: ");
                unitCost = console.nextDouble();
                while (unitCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    unitCost = console.nextInt();
                }

                System.out.println("Inventory cost: ");
                inventoryCost = console.nextDouble();
                while (inventoryCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    inventoryCost = console.nextInt();
                }

                System.out.println("Selling price: ");
                sellingPrice = console.nextDouble();
                while (sellingPrice <= 0) {
                    System.out.println("Please put in a positive integer.");
                    sellingPrice = console.nextInt();
                }
               }
        }

    public static void main(String[] args) { 
     Interface intFace = new Interface(); 
     intFace.run(); 
        } 
    } 
Was it helpful?

Solution

You can't define method in another method. Change your code to this:

public class Interface {
    private void run() 
    {

        Scanner console = new Scanner(System.in);
        Store store1 = new Store(); // MUST DO THIS

        int demandRate, option, end;
        double setupCost, unitCost, inventoryCost;
        double sellingPrice, optimalOrder;
        String name;

        do {
            System.out.println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
            option = console.nextInt();
            switch(option)
            {
            case 0: enterData();
                    break;
            case 1:
                    break;
            case 2:
                    break;
            case 9: System.out.println("You chose to exit the program.");
                    break;
            default: System.out.println("Please choose a valid option.");
            }
        } while (option != 9);
    }
    private static void enterData()
    {
        int demandRate, option, end;
        double setupCost, unitCost, inventoryCost;
        double sellingPrice, optimalOrder;
        Scanner console = new Scanner(System.in);

        System.out.println("Product name between 3 & 10 characters long: ");
        String name = console.nextLine();
        while ((name.length() < 3) || (name.length() > 10)) {
            System.out.println("Please put in a name between 3 & 10 characters long.");
            name = console.nextLine();
        }           
        name = name.toLowerCase();

        System.out.println("Demand rate: ");
        demandRate = console.nextInt();
        while (demandRate <= 0) {
            System.out.println("Please put in a positive integer.");
            demandRate = console.nextInt();
        }

        System.out.println("Setup cost: ");
        setupCost = console.nextDouble();
        while (setupCost <= 0) {
            System.out.println("Please put in a positive number.");
            setupCost = console.nextInt();
        }

        System.out.println("Unit cost: ");
        unitCost = console.nextDouble();
        while (unitCost <= 0) {
            System.out.println("Please put in a positive number.");
            unitCost = console.nextInt();
        }

        System.out.println("Inventory cost: ");
        inventoryCost = console.nextDouble();
        while (inventoryCost <= 0) {
            System.out.println("Please put in a positive number.");
            inventoryCost = console.nextInt();
        }

        System.out.println("Selling price: ");
        sellingPrice = console.nextDouble();
        while (sellingPrice <= 0) {
            System.out.println("Please put in a positive integer.");
            sellingPrice = console.nextInt();
        }
       }


    public static void main(String[] args) { 
     Interface intFace = new Interface(); 
     intFace.run(); 
     } 

}

OTHER TIPS

Try making a separate method and make those fields global. Something like this

import java.util.Scanner;

public class Interface {

    int demandRate, option, end;
    double setupCost, unitCost, inventoryCost;
    double sellingPrice, optimalOrder;
    String name;

    private void run() {

        Scanner console = new Scanner(System.in);
        Store store1 = new Store(); // MUST DO THIS
        do {
            System.out
                    .println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
            option = console.nextInt();
            switch (option) {
            case 0:
                enterData(console);
                break;
            case 1:
                break;
            case 2:
                break;
            case 9:
                System.out.println("You chose to exit the program.");
                break;
            default:
                System.out.println("Please choose a valid option.");
            }
        } while (option != 9);
    }

    private void enterData(Scanner console) {
        System.out.println("Product name between 3 & 10 characters long: ");
        name = console.nextLine();
        while ((name.length() < 3) || (name.length() > 10)) {
            System.out
                    .println("Please put in a name between 3 & 10 characters long.");
            name = console.nextLine();
        }
        name = name.toLowerCase();

        System.out.println("Demand rate: ");
        demandRate = console.nextInt();
        while (demandRate <= 0) {
            System.out.println("Please put in a positive integer.");
            demandRate = console.nextInt();
        }

        System.out.println("Setup cost: ");
        setupCost = console.nextDouble();
        while (setupCost <= 0) {
            System.out.println("Please put in a positive number.");
            setupCost = console.nextInt();
        }

        System.out.println("Unit cost: ");
        unitCost = console.nextDouble();
        while (unitCost <= 0) {
            System.out.println("Please put in a positive number.");
            unitCost = console.nextInt();
        }

        System.out.println("Inventory cost: ");
        inventoryCost = console.nextDouble();
        while (inventoryCost <= 0) {
            System.out.println("Please put in a positive number.");
            inventoryCost = console.nextInt();
        }

        System.out.println("Selling price: ");
        sellingPrice = console.nextDouble();
        while (sellingPrice <= 0) {
            System.out.println("Please put in a positive integer.");
            sellingPrice = console.nextInt();
        }
    }

    public static void main(String[] args) {
        Interface intFace = new Interface();
        intFace.run();
    }
}

Interface is some kind of an abstract class definition keyword in java. You can use a keyword to name your class with capitalized letters, but seriously, don't do this.

And you are not calling a method, you are implementing it in another method. You should go over writing and calling a method in java once again ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top