1

For an assignment I'm currently working on, I've encountered a brick wall with data classes and using a second .java file to grab class names from and input data and values to.

I've attempted using the original String and int names that are set to private, on top of constructor names and getters + setters.

The main file I'm trying to input data into:

public class HighScores {

    /*
     * Method for all content related to reading the gamescore.java file
     */
    public static void readGameScore(Scanner in) {
        GameScore gScore;

        System.out.println("Please Enter your Username");
        gScore.getUserName = sc.next();        
        }

Secondary (data class file) I'm trying to grab set values in

public class GameScore {
    //Variable Declarations
    private String Username; // The users' Username/nickname
    private int gameId;      // The ID of the game
    private int score;       // The users score
    private String title;    //Title, dependant on the score

    //Constructor (initialise variable values)
    public GameScore(String userName, int gID, int scoreValue, String scoreTitle) {
        Username = userName;
        //gameId = gID;
        //score = scoreValue;
        //title = scoreTitle;
    }

    /* Getters and Setters */

    //Username Getter + Setter
    public String getUsername() {
        return Username;
    }

    public void setUsername (String userName) {
        Username = userName;
    }
3
  • Welcome to Stackoverflow! Can you explain, what problem you have in this code and what your question is? Commented Oct 18, 2019 at 10:42
  • Thank you, and yes I can. My main error is Error: cannot find symbol symbol: variable getUserName location: variable gScore of type GameScore I'm trying to link the variables from the secondary file to the main file. IE link Username from the private String (which I know is then expanded upon in public GameScore, then in getter and setter). But i'm not sure about the error I'm getting and the variable/symbol not being found Commented Oct 18, 2019 at 10:47
  • 2
    1. Your missing an empty constructor 2. You don't create an instance of GameScore, you only declare a variable. 3. You should replace gScore.getUserName = sc.next(); with gScore.setUsername(sc.next()); Commented Oct 18, 2019 at 10:58

1 Answer 1

3

There are many problems with this code. First of all you cannot use this:

gScore.getUserName = sc.next(); 

getUserName is method, not a property. If you wish to set this value you should do this by a setter. Something like this:

gScore.setUserName(sc.next());

Or you could set this value like this: (note this requeries non-private access)

gScore.username = sc.next();

What's more - you never initialize this object gScore. You have to do this before you try to set it's values. Your current construcor is maaybe not the best solution. Remove it and use default one:

GameScore gScore = new GameScore();

Then you can set all values by setter methods.

Hope it helps a bit

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.