Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java Variable Issue

public Entity(boolean hostile, String name, int hp, int def, int atk, int exp, int gold)
    {
        this.hostile = hostile;
        this.name = name;
        this.hp = hp;
        this.def = def;
        this.atk = atk;
        this.exp = exp;
        this.gold = gold;

        if (this.hp != 125) {
            oldName = this.name;
            oldHp = this.hp;
            oldAtk = this.atk;
            oldDef = this.def;
            oldExp = this.exp;
            oldGold = this.gold;
        }
public String toString()

    {
        return "\nA " + this.name.toUpperCase() + " HAS APPEARED!\n\n "
                + this.name + " HP: " + this.hp + "\n " + this.name + " DEF: "
                + this.def + "\n " + this.name + " ATK: " + this.randomATK + " ";

Why is it that I can't set the oldName?? My player stat for hp is 125, and the monsters are never 125.

private static Random randomGenerator;
    private static ArrayList<Entity> hostiles;
    private static boolean gameStop = false;

    public static void main (String[] args)
    {
        hostiles = new ArrayList<Entity>();
        randomGenerator = new Random();

        hostiles.add(new Cyclops());
        hostiles.add(new Golem());
        hostiles.add(new Revenant());
        hostiles.add(new Vampire());
        hostiles.add(new Werewolf());
        hostiles.add(new Zombie());

        int index = randomGenerator.nextInt(hostiles.size());
        Entity entity = hostiles.get(index);
        Player player = new Player();
        player.gameStart();

        do {
                gameStop = true;
            player.gameStory();
            System.out.println(entity.toString());
            player.gameATK();

        } while (gameStop == false);

    }

My main file ^

2 Answers

Steven Parker
Steven Parker
229,786 Points

I feel a bit like "Captain Obvious" here, but you said "My player stat for hp is 125", and the code has:

        if (this.hp != 125) {
            oldName = this.name;  // do this only when hp is NOT 125

Wouldn't that be the reason it does not get set?

If that's not it, you may need to show more of the code. Please consider doing it with a link to a repo or workspace snapshot to save space here.

I deleted my last statement after looking at your code again.

if (this.hp != 125)

this means that any number above or below will make this true as long as it is not 125 hp.