1

Pre Edit: The problem is when I mark it as static, so

public static int printMenuGetSelection()

it gives me the message

This Static method cannot hide the instance method from AMenu

I'm writing a Java program that reads files and gives the user multiple options for displaying things about the file. I'm currently writing a menu interface that implements an actual Interface and makes the program easier to use. However, I'm getting an exception when I try to call the menu method in my main method. The error is on the one active line in the main method where I call printMenuGetSelection(), and it says

Cannot make static reference to the non-static method printMenuGetSelection() from the type SpecialAssignment1

How do I fix this bug? here is my program:

import java.util.*;
import java.io.*;
import java.text.*;

public class SpecialAssignment1 implements AMenu {
public static void main(String[] args) throws FileNotFoundException{
    printMenuGetSelection();
    /*System.out.println(RewardCustomer("transactions1.dat")); //CURRENTLY DISPLAYING TOP 6, DOESN'T WORK WITH TIES OR TOPN < lines
    ProcessTransactionsFile("transactions2.dat", 52);*/
}
public int printMenuGetSelection() throws FileNotFoundException{
    boolean runProgram = true;
    Scanner s = new Scanner(System.in);
    printStartMenu();
    String startMenuSelection = s.next();
    while(runProgram){
        if(startMenuSelection.equals("1")){
            startMenu1();
        } else if(startMenuSelection.equals("2")){
            startMenu2();
        } else if(startMenuSelection.equals("3")){
            startMenu3();
        } else if(startMenuSelection.equals("4")){
            startMenu4();
        } else if(startMenuSelection.equals("5")){
            runProgram = false;
        } else {
            System.out.println("***Selection Invalid!***");
        }
    }
    return 1;
}

public static void printStartMenu(){
    System.out.println("**********************************************************");
    System.out.println("Main Menu...");
    System.out.println("    (1)  RewardCustomers");
    System.out.println("    (2)  ProcessTransactionFiles");
    System.out.println("    (3)  TopCustomers");
    System.out.println("    (4)  QueryStatsFile");
    System.out.println("    (5)  Quit");
    System.out.println("        Enter a valid selection: ");
    }

public static void startMenu1() throws FileNotFoundException{
    boolean runMenu1 = true;
    while(runMenu1){
        Scanner s = new Scanner(System.in);
        System.out.println("Reward Customers Menu...");
        System.out.println("    (1)  Use transactions1.dat");
        System.out.println("    (2)  Use transactions2.dat");
        System.out.println("    (3)  Quit");
        System.out.println("        Enter a valid selection: ");
        String menu1Selection = s.next();
        if(menu1Selection.equals("1")){
            System.out.println(RewardCustomer("transactions1.dat"));
        } else if(menu1Selection.equals("2")){
            System.out.println(RewardCustomer("transactions2.dat"));
        } else if(menu1Selection.equals("3")){
            runMenu1 = false;
        } else {
            System.out.println("***Selection Invalid!***");
        }
    }
}

public static void startMenu2(){
    boolean runMenu2 = true;
    while(runMenu2){
        Scanner s = new Scanner(System.in);
        System.out.println("Process Transaction Files Menu...");
        System.out.println("    (1)  Create transactions2.dat file");
        System.out.println("    (2)  Display transactions1.dat");
        System.out.println("    (3)  Display transactions2.dat");
        System.out.println("    (4)  Query transactions1.dat");
        System.out.println("    (5)  Query transactions2.dat");
        System.out.println("    (6)  Quit");
        System.out.println("        Enter a valid selection: 4");
        String menu2Selection = s.next();
        if(menu2Selection.equals("1")){

        }
    }
}

public static void startMenu3(){

}

public static void startMenu4(){

}

I removed the code not pertaining to the question to make it easier to read, if it's needed I'll put it in. Also, here is the AMenu Interface. Please do not suggest any other changes to my program. If you think it's dumb to have the menu as an Implemented Interface, I 100% agree with you but that's the requirement. For reference, here is the AMenu Interface:

import java.io.FileNotFoundException;

public interface AMenu {
/**
 * Prints a menu with selections and logic to return a valid selection.
 * @return the selected item
 */
abstract int printMenuGetSelection() throws FileNotFoundException;

 /**
 * @return the numberOfMenuItems
 */
abstract int getNumberOfMenuItems();
}
  • It's not a duplicate, the answers to that question were highly specified toward his question and did not help me fix my problem. – user3689711 Jun 05 '14 at 04:08
  • I beg to differ. The answers to that question are only using his code because it was easiest to make an example from those. The reason behind the error is definitely something that applies here. – Dennis Meng Jun 05 '14 at 04:11

3 Answers3

0

Since printMenuGetSelection() is non static, you cannot call it from within the static method main() unless you create an instance of SpecialAssignment1 and call the method on that object.

Jason
  • 11,744
  • 3
  • 42
  • 46
0

As other people have said, you need to create an instance of SpecialAssignment1, then call printMenuSelection() on it. Part of what's making this confusing though is that you've stuck the main method inside the menu interface class. This whole thing would make more sense if you had a class SpecialAssignment1 with just the main method and a separate MenuGenerator class with all the menu generation stuff.

Kestrel12
  • 116
  • 7
0

you need to create an instance of your SpecialAssignment1 then call the method from that, as abstract requires an object.

Dispersia
  • 1,436
  • 9
  • 23