Albert Dong
Albert Dong

Reputation: 99

Java Cannot find symbol in same for-loop

In the following code, the variable number cannot be found in the line number++ even though I initialized number in the same for-loop. Can anyone tell me why?

import java.lang.Math;
import java.util.Random;

public class test
{
    public static void main( String [] args )
    {
        String cardNumber; 
        double cardRandom;
        int cardRandomNumber;
        String[] cardSelection = new String[10];

        for (int number = 0; number <=  9; );
        {
            Random ran = new Random();
            cardRandom = ran.nextInt(52 - 1 + 1) + 1;
            cardRandomNumber = (int) Math.round( cardRandom );

            if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )
            { 
                cardNumber =  "card" + cardRandomNumber;
                System.out.println( cardNumber );
                number++; // Says Error: Cannot find symbol
            }
        }   
    }
}

This program basically just picks a random number from 1-52, adds "cards" to the beginning of it and prints it out. It should print out 10 numbers.

Upvotes: 1

Views: 14284

Answers (4)

user1639637
user1639637

Reputation: 29

import java.lang.Math; 
import java.util.Random; 
public class foo 
{
    public static void main( String [] args ) 
    {

        String cardNumber;          
        double cardRandom;       
        int cardRandomNumber;       
        String[] cardSelection = new String[10];   
        for (int number = 0; number <=  9; )
        {
            Random ran = new Random();   
            cardRandom = ran.nextInt(52 - 1 + 1) + 1;         
            cardRandomNumber = (int) Math.round( cardRandom );    



            if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )          
            {     
                cardNumber =  "card" + cardRandomNumber;               
                System.out.println( cardNumber );      
            }
            number++; 
        }
    }   
}

The output is a set of 10 items:(notice that every time you run the program ,it gives different card numbers)

card2 card12 card37 card23 card18 card20 card21 card45 card19 card13

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340753

The last semicolon in the following line must go away:

for (int number = 0; number <=  9; );

You basically declare a for loop without body. Good IDE should warn you about such bugs. And BTW incrementing loop counter should go to the last for expression where we all expect it to be:

for (int number = 0; number <=  9; ++number) {
  //..
}

BTW after eliminating unnecessary variables and conditions that are always met your code looks like this:

public static void main(String[] args) {
    Random r = new Random();
    for (int number = 0; number <= 9; ++number) {
        int cardRandom = 1 + r.nextInt(52);
        String cardNumber = "card" + cardRandom;
        System.out.println(cardNumber);
    }
}

Seriously.

Upvotes: 9

pb2q
pb2q

Reputation: 59617

You've inadvertently terminated your for loop with a trailing semicolon:

for (int number = 0; number <=  9; );

This makes it equivalent to:

for (int number = 0; number <=  9; )
{ ; }

{
    Random ran = new Random();
    cardRandom = ran.nextInt(52 - 1 + 1) + 1;
    cardRandomNumber = (int) Math.round( cardRandom );

    // etc. ...

And so the number variable is out of scope when you increment it.

Remove that trailing semicolon.

Upvotes: 2

duffymo
duffymo

Reputation: 308793

Remove the semicolon:

for (int number = 0; number <=  9; ); // <-- this is your problem

Upvotes: 1

Related Questions