Question

I've got the following code:

    private static void createMenu(String nameOfMenu, Callable<Void> case1, Callable<Void> case2, Callable<Void> case3 )  throws Exception{
            System.out.println(nameOfMenu+" Options");
            System.out.println("What would you like to do?(Type the appropriate slection number)");
            System.out.println("(1) Add "+nameOfMenu);
            System.out.println("(2) Edit "+nameOfMenu);
            System.out.println("(3) Remove "+nameOfMenu);
            System.out.println("(4) Return to Main Menu");
            System.out.println("To quit the program, type 'Q'");
            String input = scanner.nextLine().toLowerCase();
            while (!"1".equals(input) && !"2".equals(input) && !"3".equals(input) && !"4".equals(input) && !"q".equals(input)) {
                System.err.println("Please enter 1, 2, 3, 4 or 'q' only");
                input = scanner.nextLine().toLowerCase();
            }
            switch (input) {
                case "1":
case1.call();
                    break;
                case "2":
case2.call();
                    break;
                case "3":
case3.call();
                    break;
                case "4":
                    mainMenu();
                    break;
                case "q":
System.exit(0);
                    System.out.println("Thank you!");
                    break;
            }
        }

I've got a second method that is trying to call the method above

private static void callingMethod(){
createMenu("menu name",method(),method1(),method2());
}

all the methods are static void. Netbeans gives me an error saying 'void' type is not allowed here. how can i fix this error?

Was it helpful?

Solution

Callable is not a method. It is an interface. So try something like this:

class MyTask implements Callable<Void> {
    @Override
    public Void call() throws Exception {
        // do something

        return null;
    }
}

...

private static void callingMethod() {
    createMenu("menu name", new MyTask(), new MyTask(), new MyTask());
}

OTHER TIPS

If readability is not a priority, you can use anonymous classes:

createMenu("menu name",
     new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            method();
            return null;
        }

    },
    new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            method1();
            return null;
        }

    },
     new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            method2();
            return null;
        }

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