0

I have three classes called Pokemon.java, Move.java and PokemonTrainer.java which contain methods for creating and modifying Pokemon, their moves and their trainers. I've created all of the required methods, but I'm having issues calling the Pokemon objects that I have created within the PokemonSimulation.java class, the program always fails to compile with the error "java:56: error: cannot find symbol". How can I fix this?

Here is the code for Pokemon.java:

import java.util.ArrayList;
public class Pokemon
{
    // Copy over your code for the Pokemon class here
    // Private constants
    private static final int MAX_HEALTH = 100;
    private static final int MAX_MOVES = 4;
    private String name;
    private int health;
    private int opponentHealth;
    public static int numMovesForPokemon = Move.getNumOfMoves();
    private Move move;
    private ArrayList<Move> moveListForPokemon;
    private String pokemonImage;

    // Write your Pokemon class here
    public Pokemon(String theName, int theHealth)
    {
        name = theName;
        if(theHealth <= MAX_HEALTH)
        {
            health = theHealth;
        }
        moveListForPokemon = new ArrayList<Move>();
    }

    public Pokemon(String name, String image)
    {
        this.name = name;
        health = 100;
        pokemonImage = image;
    }

    public Pokemon(String theName)
    {
        name = theName;
        health = 100;
        pokemonImage = theName;
    }    

    public void setImage(String image)
    {
        pokemonImage = image;
    }

    public String getImage()
    {
        return pokemonImage;
    }

    public String getName()
    {
        return name;
    }

    public int getHealth()
    {
        return health;
    }

    public void setHealth(int health)
    {
        this.health = health;
    }    

    public boolean hasFainted()
    {
        if(health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean canLearnMoreMoves()
    {
        if(numMovesForPokemon < 4)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean learnMove(Move other)
    {
        if(canLearnMoreMoves())
        {
            moveListForPokemon.add(other);
            numMovesForPokemon++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void forgetMove(Move other)
    {
            moveListForPokemon.remove(other);
    }

    public ArrayList<Move> getMoves()
    {
        return moveListForPokemon;
    }

    public boolean knowsMove(Move move)
    {
        if(moveListForPokemon.contains(move))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean knowsMove(String moveName)
    {
        int doesListContainName = 0;
        for(Move m : moveListForPokemon)
        {
            if(m.getName() != null && m.getName().equals(moveName))
            {
                doesListContainName = 1;
            }

        }

        if(doesListContainName == 1)
        {
            doesListContainName = 0;
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, Move move)
    {
        if(knowsMove(move))
        {
            opponentHealth = opponent.getHealth();
            opponentHealth -= move.getDamage();
            opponent.setHealth(opponentHealth);
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, String moveName)
    {
        if(knowsMove(moveName))
        {
            opponentHealth = opponent.getHealth();
            for(Move m : moveListForPokemon)
            {
                if(m.getName() != null && m.getName().equals(moveName))
                {
                    opponentHealth -= m.getDamage();
                }
            }
            opponent.setHealth(opponentHealth);
            return true;
        }
        else
        {
            return false;
        }
    }    

    public String toString()
    {
        return pokemonImage + "\n" + name + " (Health: " + health + " / " + MAX_HEALTH + ")";
    }
    // Add the methods specified in the exercise description
}

Here is the code for Move.java:

import java.util.ArrayList;
public class Move
{
    // Copy over your code for the Move class here
    private static final int MAX_DAMAGE = 25;
    private String name;
    private int damage;
    public static int numMoves;
    private ArrayList<Move> moveList = new ArrayList<Move>();

    public Move(String theName, int theDamage)
    {
        name = theName;
        if(theDamage <= MAX_DAMAGE)
        {
            damage = theDamage;
        }
    }

    public String getName()
    {
        return name;
    }

    public int getDamage()
    {
        return damage;
    }

    public static int getNumOfMoves()
    {
        return numMoves;
    }

    public ArrayList<Move> getList()
    {
        return moveList;
    }    

    public String toString()
    {
        return name + " (" + damage + " damage)";
    }    
    // Add an equals method so we can compare Moves against each other

    public boolean equals(Move other)
    {
        if(name.equals(other.getName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }     
}

Here is the code for PokemonTrainer.java:

import java.util.ArrayList;
public class PokemonTrainer
{
    // private constants
    private static final int MAX_POKEMON = 2;
    private ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
    private String name;
    private Pokemon p;
    private int numOfPokemon;

    // Write your PokemonTrainer class here
    public PokemonTrainer(String name)
    {
        this.name = name;
    }

    public boolean addPokemon(Pokemon p)
    {
        if(numOfPokemon < MAX_POKEMON)
        {
            pokemonList.add(p);
            numOfPokemon++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean hasLost()
    {
        int numOfPokemonThatLost = 0;
        for(Pokemon p : pokemonList)
        {
            if(p.hasFainted())
            {
                numOfPokemonThatLost++;
            }
        }
        if(numOfPokemonThatLost == numOfPokemon)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public Pokemon getNextPokemon()
    {
        int nextPokemon = 0;
        int numOfPokemonThatLost = 0;
        for(Pokemon p : pokemonList)
        {
            while(p.hasFainted() && nextPokemon < numOfPokemon)
            {
                nextPokemon++;
                numOfPokemonThatLost++;
                if(nextPokemon < numOfPokemon)
                {
                    p = pokemonList.get(nextPokemon);
                }
            }
        }
        if(numOfPokemonThatLost == numOfPokemon)
        {
            return null;
        }
        else
        {
            return pokemonList.get(nextPokemon);
        }
    }

    public String toString()
    {
        return name;
    }
}

Finally, here is the code for PokemonSimulation.java:

public class PokemonSimulation extends ConsoleProgram
{
    private PokemonImages images = new PokemonImages();
    private String trainerName;
    private String trainerName2;
    private String pokemonImage;
    private String pokemonName;
    private String newMove;
    private String moveName;
    private int moveDamage;
    private int numOfMoves1;
    private int numOfMoves2;
    private int numOfMoves3;
    private int numOfMoves4;

    public void run()
    {
        System.out.println("Welcome the Pokemon Simulator!");
        System.out.println("");
        // First trainer
        System.out.println("Set up first Pokemon Trainer:");
        trainerName = readLine("Trainer, what is your name? ");
        PokemonTrainer pt1 = new PokemonTrainer(trainerName);
        System.out.println("Hello " + pt1 + "!");
        System.out.println("");
        System.out.println("Choose your first pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p1 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p1);
        }
        else
        {
            Pokemon p1 = new Pokemon(pokemonName);
            System.out.println(p1);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves1 < 4)
            {
                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves1 == 0)
                {
                    Move m1 = new Move(moveName, moveDamage);
                    p1.learnMove(m1);
                    System.out.println(pokemonName + " learned " + m1);
                }
                else if(numOfMoves1 == 1)
                {
                    Move m2 = new Move(moveName, moveDamage);
                    p1.learnMove(m2);
                    System.out.println(pokemonName + " learned " + m2);
                }
                else if(numOfMoves1 == 2)
                {
                    Move m3 = new Move(moveName, moveDamage);
                    p1.learnMove(m3);
                    System.out.println(pokemonName + " learned " + m3);
                }
                else if(numOfMoves1 == 3)
                {
                    Move m4 = new Move(moveName, moveDamage);
                    p1.learnMove(m4);
                    System.out.println(pokemonName + " learned " + m4);
                }
                numOfMoves1++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves1 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves1 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        System.out.println("Choose your second pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p2 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p2);
        }
        else
        {
            Pokemon p2 = new Pokemon(pokemonName);
            System.out.println(p2);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves2 < 4)
            {

                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves2 == 0)
                {
                    Move m5 = new Move(moveName, moveDamage);
                    p2.learnMove(m5);
                    System.out.println(pokemonName + " learned " + m5);
                }
                else if(numOfMoves2 == 1)
                {
                    Move m6 = new Move(moveName, moveDamage);
                    p2.learnMove(m6);
                    System.out.println(pokemonName + " learned " + m6);
                }
                else if(numOfMoves2 == 2)
                {
                    Move m7 = new Move(moveName, moveDamage);
                    p2.learnMove(m7);
                    System.out.println(pokemonName + " learned " + m7);
                }
                else if(numOfMoves2 == 3)
                {
                    Move m8 = new Move(moveName, moveDamage);
                    p2.learnMove(m8);
                    System.out.println(pokemonName + " learned " + m8);
                }
                numOfMoves2++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves2 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves2 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        // End of first trainer


        // Second trainer
        System.out.println("Set up second Pokemon Trainer:");
        trainerName = readLine("Trainer, what is your name? ");
        PokemonTrainer pt2 = new PokemonTrainer(trainerName);
        System.out.println("Hello " + pt2 + "!");
        System.out.println("");
        System.out.println("Choose your first pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p3 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p3);
        }
        else
        {
            Pokemon p3 = new Pokemon(pokemonName);
            System.out.println(p3);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves3 < 4)
            {
                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves3 == 0)
                {
                    Move m9 = new Move(moveName, moveDamage);
                    p3.learnMove(m9);
                    System.out.println(pokemonName + " learned " + m9);
                }
                else if(numOfMoves3 == 1)
                {
                    Move m10 = new Move(moveName, moveDamage);
                    p3.learnMove(m10);
                    System.out.println(pokemonName + " learned " + m10);
                }
                else if(numOfMoves3 == 2)
                {
                    Move m11 = new Move(moveName, moveDamage);
                    p3.learnMove(m11);
                    System.out.println(pokemonName + " learned " + m11);
                }
                else if(numOfMoves3 == 3)
                {
                    Move m12 = new Move(moveName, moveDamage);
                    p3.learnMove(m12);
                    System.out.println(pokemonName + " learned " + m12);
                }
                numOfMoves3++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves3 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves3 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        System.out.println("Choose your second pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p4 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p4);
        }
        else
        {
            Pokemon p4 = new Pokemon(pokemonName);
            System.out.println(p4);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves4 < 4)
            {

                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves4 == 0)
                {
                    Move m13 = new Move(moveName, moveDamage);
                    p4.learnMove(m13);
                    System.out.println(pokemonName + " learned " + m13);
                }
                else if(numOfMoves4 == 1)
                {
                    Move m14 = new Move(moveName, moveDamage);
                    p4.learnMove(m14);
                    System.out.println(pokemonName + " learned " + m14);
                }
                else if(numOfMoves4 == 2)
                {
                    Move m15 = new Move(moveName, moveDamage);
                    p4.learnMove(m15);
                    System.out.println(pokemonName + " learned " + m15);
                }
                else if(numOfMoves4 == 3)
                {
                    Move m16 = new Move(moveName, moveDamage);
                    p4.learnMove(m16);
                    System.out.println(pokemonName + " learned " + m16);
                }
                numOfMoves4++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves4 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves4 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        // End of second trainer
        //Battle
        System.out.println("You have finished setting up the Pokemon and their trainers. \nGet ready for battle!");
        System.out.println("");
        System.out.println(pt1 + " brings out " + p1 + " to battle!");
        System.out.println(pt2 + " brings out " + p2 + " to battle!");
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
        {
            if(p1.getHealth() <= 100)
            {
                System.out.println(p1.getMoves());
                String p1Attack = readLine(pt1 + ", what attack do you want " + p1 + " to use? ");

            }
        }
    }
}

Here is the error log that shows up when I try to run this code:

PokemonSimulation.java:50: error: cannot find symbol
                    p1.learnMove(m1);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:56: error: cannot find symbol
                    p1.learnMove(m2);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:62: error: cannot find symbol
                    p1.learnMove(m3);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:68: error: cannot find symbol
                    p1.learnMove(m4);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:120: error: cannot find symbol
                    p2.learnMove(m5);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:126: error: cannot find symbol
                    p2.learnMove(m6);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:132: error: cannot find symbol
                    p2.learnMove(m7);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:138: error: cannot find symbol
                    p2.learnMove(m8);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:198: error: cannot find symbol
                    p3.learnMove(m9);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:204: error: cannot find symbol
                    p3.learnMove(m10);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:210: error: cannot find symbol
                    p3.learnMove(m11);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:216: error: cannot find symbol
                    p3.learnMove(m12);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:268: error: cannot find symbol
                    p4.learnMove(m13);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:274: error: cannot find symbol
                    p4.learnMove(m14);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:280: error: cannot find symbol
                    p4.learnMove(m15);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:286: error: cannot find symbol
                    p4.learnMove(m16);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:317: error: cannot find symbol
        System.out.println(pt1 + " brings out " + p1 + " to battle!");
                                                  ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:318: error: cannot find symbol
        System.out.println(pt2 + " brings out " + p2 + " to battle!");
                                                  ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
               ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                                           ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                                                            ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:321: error: cannot find symbol
            if(p1.getHealth() <= 100)
               ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:323: error: cannot find symbol
                System.out.println(p1.getMoves());
                                   ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:324: error: cannot find symbol
                String p1Attack = readLine(pt1 + ", what attack do you want " + p1 + " to use? ");
                                                                                ^
  symbol:   variable p1
  location: class PokemonSimulation
25 errors

1 Answers1

0

Your Pokemon p1 = ... is limited to the scope of its surrounding if(...){}. Declare it in a higher scope.

  • The reason I'm including it in an if statement is because I have two constructors that take different inputs, the if statement checks if a user inputed a specific name so it can show an ASCII image of the pokemon. – pcgamer2315132 Jan 20 '20 at 02:35
  • @pcgamer2315132 declaring a variable is different thing from initializing it. Declare higher, initalize in constructor. – itwasntme Jan 20 '20 at 02:40
  • @pcgamer2315132 That's fine, you can use those if to initialize the variable(s). But you need to put `Pokemon pX;` somewhere in a higher scope, so that the variable is known. – Thomas Lehner Jan 20 '20 at 02:42