Somewhat
Somewhat

Reputation: 31

'Cannot Find Symbol' error when using a 'for' loop

I am a complete beginner with Java and am having trouble understanding how to pass objects around between classes and methods. I have made some progress, however my app now fails when I try to create playing cards inside a for loop. It works fine if I remove the loop. Here is the first part of the class that contains the error:

public class Testing
{
    public static void main(String[] args)
    {

   int Deal = 1;

   for(int Hand = 0; Hand < Deal; ++Hand)
   {
     //instantiate and derive values for Player
     Card card1 = new Card();
     card1.setSuit();  //assign card 1's suit
     card1.setValue(); //asign card 1's value

     //instantiate and derive values for Computer
     Card card2 = new Card();
     card2.setSuit();  //assign card 2's suit
     card2.setValue(); //assign card 2's suit

     //compare the two cards and make sure they are different
     cardCompare(card1,card2);
   }

   //output the two cards to the screen
   output(card1,card2);

}

This is the error I get:

Testing.java:26: error: cannot find symbol
   output(card1,card2);
          ^
  symbol:   variable card1
  location: class Testing

Testing.java:26: error: cannot find symbol
   output(card1,card2);
                ^
  symbol:   variable card2
  location: class Testing
2 errors

Since the code works if I remove the for loop, I'm assuming that somehow the names card1 and card2 are not visible outside the loop? If I wanted to create ten or twenty cards, I'd want to do that in a loop, so I must be missing something about instantiating new objects and using them elsewhere in the program.

Thanks for your help.

**Update: thanks for the initial feedback. I see now that if I move my instantiate statements outside the for loop, I could theoretically assign new values for those objects over and over again using a loop, which is all I need to complete this particular task.

I'm still curious though, is it not possible to instantiate new objects inside a loop, but still use them outside the loop? It seems like this must be possible somehow.

public class Testing
   {
    public static void main(String[] args)
   {

int Deal = 1;

   //instantiate and derive values for Player
     Card card1 = new Card();

      //instantiate and derive values for Computer
     Card card2 = new Card();

   for(int Hand = 0; Hand < Deal; ++Hand)
   {
     card1.setSuit();  //assign card 1's suit
     card1.setValue(); //asign card 1's value

     card2.setSuit();  //assign card 2's suit
     card2.setValue(); //assign card 2's value

    //compare the two cards and make sure they are different
     cardCompare(card1,card2);
   }


   //output the two cards to the screen
   output(card1,card2);


}

Upvotes: 3

Views: 3327

Answers (3)

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

Well, as it stands, its going to keep trying to overwrite card1 and card2 since you'll both declare and initialize them "deal" times. Additionally, and more importantly, they'll be out of scope. Instead, declare it beforehand and only initialize them inside the loop.

What you probably want here is:

public class Testing
   {
    public static void main(String[] args)
   {

 int Deal = 1;

 ArrayList<Card> playerCards = new ArrayList<Card>();
 ArrayList<Card> computerCards = new ArrayList<Card>();

  //instantiate and derive values for Player
 Card card1;

 //instantiate and derive values for Computer
 Card card2;

 for(int Hand = 0; Hand < Deal; ++Hand)
 {
    card1 = new Card();
    card1.setSuit();  //assign card 1's suit
    card1.setValue(); //asign card 1's value

    card2 = new Card();
    card2.setSuit();  //assign card 2's suit
    card2.setValue(); //assign card 2's value



   //compare the two cards and make sure they are different
   cardCompare(card1,card2);

   playerCards.Add(card1);
   computerCards.Add(card2);

}

  //output the two cards to the screen

  output(card1,card2);

}

Haven't tested it, but it should work.

You also need to rethink your use of your Output method, too. Since you're going to have ~20 cards per person, when do you think you need to show them to the user? Currently, you have it outside of the for loop, so it's only going to display the LAST value assigned to each one. If you want to show them each card they're getting, put the output call inside the for loop, and maybe use Thread.Sleep() to pause the program for a half second or so so that they can see each card they get. That, or write an overload of output that accepts an ArrayList of cards, and prints all of them at the same time. Once again, if you need help with that, ask me.

By the way, im not sure what cardcompare() is really doing behind the scenes, but you probably want to make it return a bool representing whether or not they were different. something like:

bool areCardsDistinct = cardcompare(card1,card2);

That way, you can use the result to decide whether or not to get random new cards again

Upvotes: 0

Makoto
Makoto

Reputation: 106440

Both card1 and card2 are in scope of the for loop, not the rest of main(). Move your initialization to before the for.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

The card1 and card variables are declared inside the for loop and is thus only visible inside of the loop. To use it outside of the loop, you must declare it before the loop. Please read up on Java scoping rules.

Upvotes: 5

Related Questions