Question

A user enters a guess, which is originally a string, and now I'm trying to convert that string into an array of integers, the error is saying that I cannot make a non-static reference. I don't want to make the 'userGuess' variable static, so any other option?

Player Class

import java.util.Scanner;
public class Player 
{
    public int fastestWin, gamesCompleted=1;
    public String name, setName;
    public String userGuess;

    Scanner keyboard = new Scanner(System.in);

    public String getName()
    {
        System.out.print("Enter the player's name: ");
        return name = keyboard.nextLine();
    }

    public void setName()
    {
        getName();
        setName = name;
    }

    public String askForGuess()
    {
        System.out.print("Enter your guess: ");
        return  userGuess = keyboard.nextLine();
    }

    public void gamesCount()
    {
        gamesCompleted++;
    }
}

Engine Class

import java.util.Random;
import java.util.Scanner;

public class Engine 
{
    public int numDigits, numDigitsSet;
    public int i;
    public int[] secretNumber, userGuessArray;
    public Random randomNumberGenerator;
    public String guessConvert = Player.userGuess;  //Made new variable 'guessConvert'                   
                                                    //getting error here for static reference
    Scanner sc = new Scanner(System.in);

    public void setNumDigits()
    {
        numDigitsSet = numDigits;
    }

    public int getNumDigits()
    {
        System.out.print("Enter the number of digits to use: ");
        return numDigits = sc.nextInt();
    }

    public void generateNewSecret()
    {
        Random rand = new Random();{
            for (int i=0; i<numDigitsSet; i++)
            {
                secretNumber[i]= rand.nextInt(10);
                System.out.println("" + secretNumber[i]);

            }
        }
    }

    public int[] getSecretNumber()
    {
        for (int j=0; j<secretNumber.length; j++)
        {
            System.out.println("" + secretNumber[j]);
        }
        return secretNumber;
    }


    public void convertNumtoDigitArray(String guessConvert)  //Trying to convert guessConvert into an array
    {

        String[] userGuessSplit = guessConvert.split(",");
        int[] userGuessArray = new int[userGuessSplit.length];
        for (int j=0; j<userGuessSplit.length; j++)
        {
            userGuessArray[j] = Integer.parseInt(userGuessSplit[j]);
        }
    }
}
Était-ce utile?

La solution

Just make another member variable, like the others

public String guessConvert;

and set that variable in a constructor.

public Engine(Player player) {

    guessConvert = player.userGuess;

}

or in a Setter Method:

public void SetGuessConvert(Player player)
{
    guessConvert = player.userGuess;
}

You might want to hold a reference to that Player object as a member variable, if you need to refer to it elsewhere in your code.

Autres conseils

The Engine object should have a reference to a Player object
and should call getUserGuess() on the Player object it points to.

It appears you have a class Player, which has a field userGuess that you say you don't want to make static. If you don't want to make it static, that means that userGuess is a field that belongs to each Player object; therefore, to access the field, you have to have a Player object to access the field. Player.userGuess will not work. It has to be p.userGuess where p is some Player object.

However, I can't help any further without knowing just where you want that Player object created, and how Engine is going to find out what that object is.

You could remove the userGuess String entirely; it doesn't seem necessary. You would then create a Player object and do guessConvert = pObject.askForGuess();

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top