Java a Beginners guide Try this 4-1 gives java.lang.NoSuchMethodError

I am trying an exercise from Java a beginners guide and it gives me the following error:

HelpClassDemo Exception in thread "main" java.lang.NoSuchMethodError: 'void Help.showMenu()' at HelpClassDemo.main(HelpClassDemo.java:72)

I have googled the error and tried to retype it to see if it was an uppercase or lowercase letter issue but it doesn't work. Then I pasted the solution from the book into the program and even that doesn't work. What is wrong in the code?

    /* Convert help system in a Help class */

    class Help {
        void helpOn (int what) {
            switch(what) {
            case '1':
            System.out.println("The if:\n");
            System.out.println("if(condition) statement;");
            System.out.println("else statement;");
            break;
            case '2':
            System.out.println("The traditional switch:\n");
            System.out.println("switch (expression) {");
            System.out.println(" case constant:");
            System.out.println(" statement sequence");
            System.out.println(" break;");
            System.out.println(" //...");
            System.out.println("}");
            break;
            case '3':
            System.out.println("The for:\n");
            System.out.print("for(init; condition; iteration)");
            System.out.println(" statement;");
            break;
            case '4':
            System.out.println("The while:\n");
            System.out.println("while(condition) statement;");
            break;
            case '5':
            System.out.println("The do-while:\n");
            System.out.println("do {");
            System.out.println(" statement;");
            System.out.println("} while (condition) ;");
            break;
            case '6':
            System.out.println("The break: \n");
            System.out.println("break; or break label;");
            break;
            case '7':
            System.out.println("The continue:\n");
            System.out.println("continue; or continoue label;");
            break;    
        }
        System.out.println();
    }
    
    void showMenu() {
        System.out.println("Help on: ");
        System.out.println(" 1. if ");
        System.out.println(" 2. switch ");
        System.out.println(" 3.for");
        System.out.println(" 4. while");
        System.out.println(" 5. do-while");
        System.out.println(" 6. break");
        System.out.println(" 7. continue\n");
        System.out.println("Choose one: (q to quit): ");
    }
    boolean isValid(int choice) {
        if(choice < '1' | choice > '7' & choice != 'q') return false;
        else return true;
    }

    }
    class HelpClassDemo {
    public static void main(String[] args) 
        throws java.io.IOException {
        char choice, ignore;
        Help hlpobj = new Help() ;

        for(;;) {
        do{
            hlpobj.showMenu();
    
            choice = (char) System.in.read();

        do{
            ignore = (char) System.in.read();
        } while(ignore != '\n');

    } while( !hlpobj.isValid(choice));

        if(choice == 'q') break;

        System.out.println("\n");

        hlpobj.helpOn(choice);
        
       }
     }
  }

1 answer

  • answered 2022-05-06 18:23 magno.io

    Greatings,

    Firstly if you are using nested class, you have to make sure that the second class is inside the main class.

    But, It seems like two diferent classes. In this case, you must define the Access Modifiers as public.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum