0

So, as you will see I have a menu system. The bit of code you're about to see shows how I intend for the user's input to determine what method/or user option in the program will be presented to them. To do this, I've tried an if statement within my menu system method which I'd like to call to the particular method which is assigned to whatever option the user has picked - to then start that option's purpose/or method. I've typed in the method I'd like option '1' to correspond to within the if statement... but it presents to me an error. Any help on how I fix this or how to go about this... would be truly appreciated. P.s. I've only been learning Java for a couple weeks, and it's my first language.

The error prints this message: error: non-static method keepCounting() cannot be referenced from a static context keepCounting(); ^ 1 error compiler exit status 1

import java.util.Scanner;

class Main {
  // instance fields
  public static final Scanner userInput = new Scanner(System.in);
  

  // constructor method (defunct)


  // 0. menu system method
    public static void menuSystem(){
      int usersNumberChoice;

      System.out.println("P4CS Mini Applications");
      System.out.println("----------------------");
      System.out.println("Please select an option:");
      System.out.println("1. Keep Counting Game");
      System.out.println("2. Number Conversion Tool");
      System.out.println("3. UPC Calculator");
      System.out.println("4. UPC Checker");
      System.out.println("9. Quit");
      System.out.println("Please enter an option:");

      usersNumberChoice = userInput.nextInt();

      if (usersNumberChoice == 1){
        keepCounting();
      }

    }


  // 1. keep counting game method
    public void keepCounting(){
      System.out.println("hello world");
    }

1 Answers1

2

Mark your keepCounting() as static because static method can not be called from non-static method.

public static void keepCounting(){
   System.out.println("hello world");
}
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24