Paincakes
Paincakes

Reputation: 147

(Java) Switch Cannot Find Symbol

I am getting an error with cannot find symbol with a switch. I've done switch before however, that was in the driver. This is my first time using a switch in my own class. Anyways here a sample of my code:

import java.util.*;
public class TrumpWar
{

   protected CardPile pl;
   protected CardPile p2;
   protected CardPile tCard;
   protected CardPile treasury;

   public TrumpWar( )
   {
        CardPile cp = new CardPile (new Card [52]);
        cp.shuffle();

        CardPile tCard = new CardPile();
        for (int i=0; i<6; i++)
            tCard.get(i);
        cp.shuffle();

        CardPile p1 = new CardPile(new Card [26]);
        CardPile p2 = new CardPile(new Card [26]);
    }

public void play()
{
    Scanner kb = new Scanner(System.in);
    do
    {
        System.out.println("At each turn, type: ");
        System.out.println("P to print");
        System.out.println("M to mix (shuffle the cards)");
        System.out.println("S to save");
        System.out.println("Q to quit");
        System.out.println("just ENTER to play a turn");

        String meunChoice = kb.nextLine();

        if(!meunChoice.equals("M") || !meunChoice.equals("m") || !meunChoice.equals("P") || !meunChoice.equals("p") || !meunChoice.equals("Q") || !meunChoice.equals("q") || !meunChoice.equals("S") || !meunChoice.equals("s") || !meunChoice.equals(str = String.valueOf(kb.nextLine())))
            throw new IllegalArgumentException ("Incorrect input, please re-enter.");
        else
        {
            switch (meunChoice)
            {
                case ("P"):
                case ("p"):     System.out.println("Player1 cards: " + p1.toString()); //<--- Cannot find p1.
                                System.out.println("Player1 cards: " + p2.toString());
//More codes...

I have no clue as to why I am getting that error when clearly I've declared p1 outside of the switch scope. Unless, there is a different way of using a switch in a class when compared to a driver.

Also, please ignore any logic errors as this is still a work in progress. I, at least, need the program to compile first before I can tackle any logic errors.

Thank you for your help!

Upvotes: 0

Views: 276

Answers (2)

Acapulco
Acapulco

Reputation: 3533

The problem seems to be that you are shadowing the p1 and p2 variables, by re-declaring them in the constructor.

Instead of

 CardPile p1 = new CardPile(new Card [26]);
 CardPile p2 = new CardPile(new Card [26]);

Try doing

 p1 = new CardPile(new Card [26]);
 p2 = new CardPile(new Card [26]);

The difference is that in the first case you are declaring a variable (since it has the class type, i.e. CardPile) and in the second case you are using the variable already declared at the top, which is what you want to do apparently.

So if you re-define the variables p1 and p2 inside the constructor, they will remain undefined outside of it, i.e. inside play() p1 and p2 are null.

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236112

The attribute is named pl, not p1, and besides p1 was declared in the TrumpWar() constructor as a local variable, clearly it can't be accessed from play(). What you have to do is this:

// outside

protected CardPile p1; // you wrote pl, rename it!

// in the constructor

p1 = new CardPile(new Card [26]);
p2 = new CardPile(new Card [26]);

Now the attributes p1 and p2 are being instantiated, in your code you were declaring a couple of local variables that happened to have (almost) the same name as the attributes - a compiler warning should have told you that.

Upvotes: 3

Related Questions