Don Patron
Don Patron

Reputation: 15

Storing User Input into a Variable

User input isn't updating the corresponding variable. I am trying to update the Dexterity variable when a user inputs a number when prompted.

I have created a setDex method that should take the user input amount and update it when added to the getDex method. The getDex method should be retrieving the current value of the variable.

public class SetAttributes {


    double Dexterity;
    double Strength;
    double Intelligence;
    double Stamina;
    double SkillPoints = 50;



    public SetAttributes() {
        this.setDex(0);
        this.setStr(0);
        this.setInt(0);
        this.setSta(0);
        this.setSkillPoints(50);
    }

    public double getSP(){
        return SkillPoints;

    }
    public double getDex() {
        return Dexterity;
    }

    public double getStr(){
        return Strength;
    }

    public double getInt(){
        return Intelligence;
    }

    public double getSta(){
        return Stamina;
    }




    public void setDex(double dexterity) {
        this.Dexterity = Dexterity;
    }
    public void setStr(double strength){
        this.Strength = Strength;
    }
    public void setInt(double intelligence){
        this.Intelligence = Intelligence;
    }
    public void setSta(double stamina){
        this.Stamina = Stamina;
    }
    public void setSkillPoints(double skillPoints) {this.SkillPoints = SkillPoints;};
}




public final class PointSpender {




    public static void Spend(){

        SetAttributes Attribute = new SetAttributes();


        Scanner SkillChoice = new Scanner(System.in);

        System.out.println("What skill do you want to increase? Dex, Str, Sta, or Int?");



        switch(SkillChoice.nextLine().charAt(0)){
            case 'd':
                System.out.println("How many points towards Dexterity?");
                System.out.println("Your current Dexterity is " + Attribute.Dexterity);

          double amount = SkillChoice.nextDouble();



                Attribute.setDex(amount + Attribute.getDex());
              System.out.println(Attribute.Dexterity);

Upvotes: 1

Views: 705

Answers (3)

S.Mandal
S.Mandal

Reputation: 182

public void setDex(double dexterity) {
    this.Dexterity = Dexterity;
}

First of all, this.Dexterity = Dexterity; must be this.Dexterity = dexterity; because dexterity is the variable that need to be assigned to Dexterity. And when you use this.Dexterity = Dexterity;, actually you are assigning the value of Dexterity to Dexterity which is actually meaningless.

All setter methods you have written, have same problem.

And finally try to maintain naming convention.

Upvotes: 0

Noctem
Noctem

Reputation: 40

All of your setter methods follow the same problem (this is why you should follow conventions like variables being camelCase), so let's pick any of them.

public void setSta(double stamina){
    this.Stamina = Stamina;
}

As you can see you set the class level variable Stamina equal to the variable Stamina. The method parameter is called stamina. So what you're setting this.Stamina to is itself.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44854

In java variables are case sensitive

this.Dexterity = Dexterity;

should be

this.Dexterity = dexterity;

else they are referring to the same variable

Upvotes: 0

Related Questions