0

This question is based on my former question. How to add a "cheat" function to a Java mastermind game I added the cheat function to my program, but it cannot compile because of "Cannot Make Static Reference to Non-Static Method"(the old codes works, you can check it through the link I post). Here are my new codes:

import java.util.*;

public class mm {
  static int[] random;
  public static void main(String[] args) {
    System.out.println("I'm thinking of a 4 digit code.");

    //update
    mm m1 = new mm();
    random = m1.numberGenerator();

    int exact=0, close=0;
    while(exact!=4){
      int[] guess= m1.userinput(); //update
      exact=0;
      close=0;
      for(int i=0;i<guess.length;i++){
        if(guess[i]==random[i]){
        exact++;
        }
        else if (random[i]==guess[0] || random[i]==guess[1] || random[i]==guess[2] || random[i]==guess[3]) {
                    close++;
        }
      }
      if(exact==4){
        System.out.println("YOU GOT IT!");
      }
      else{
        System.out.println("Exact: "+exact+" Close: "+close);
      }
    }   
  }

  public int[] userinput() {
    System.out.print("Your guess: ");
    Scanner user = new Scanner(System.in);
    String input = user.nextLine();
    //cheater
    if (input.equals("*")) {
      System.out.format("Cheater!Secret code is:");
      for(int i=0;i<random.length;i++){
        System.out.print(random[i]);
      }
    }
    int[] guess = new int[4];
    for (int i = 0; i < 4; i++) {
      guess[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
    }
    return guess;
  }

  public int[] numberGenerator() {
    Random rnd = new Random();
    int[] randArray = {10,10,10,10};
    for(int i=0;i<randArray.length;i++){
      int temp = rnd.nextInt(9);
      while(temp == randArray[0] || temp == randArray[1] || temp == randArray[2] || temp == randArray[3]){
        temp=rnd.nextInt(9);
      }
      randArray[i]=temp;
    }
    return randArray;
  }
}

How to solve this?

Community
  • 1
  • 1
turf
  • 183
  • 2
  • 2
  • 16

2 Answers2

2

You can't call a non-static method directly from a static method. in public static main(String [] args) To do so, you should first create an object of the class.

try this at main method:

mm m1 = new mm();
random = m1.numberGenerator();

int [] guess = m1.userInput();

this should work

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • Thank you. But it still have one static-nonstatic error on random = m1.numberGenerator(); Why this happens? – turf Apr 26 '15 at 21:42
  • Can you be more specific ? I mean give the exact error messege ? Also note that your question have marked as a duplicate :) – ThisaruG Apr 26 '15 at 21:43
  • Yeah, I know. But I cannot understand the answer from that question:( . Here is the error message I got "mm.java:8: Error: Cannot Make Static Reference to Non-Static Method numberGenerator() mm.numberGenerator(); ^ 1 error" – turf Apr 26 '15 at 21:50
  • Your code should be m1.numbeGenerator(); not mm.numberGenerator(); – ThisaruG Apr 26 '15 at 21:52
  • I edited it and it is able to compile now. But now the program report an error that "Error: Could not find or load main class":( I am being mad about this program...(I've updated the code to my current version so you can check it) – turf Apr 26 '15 at 22:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76305/discussion-between-thisaru-guruge-and-turf). – ThisaruG Apr 26 '15 at 22:09
0

The other option would be to make userinput method static as well

almeynman
  • 7,088
  • 3
  • 23
  • 37