www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Cannot find symbol error when trying to compile a random number generator

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    136

    resolved [RESOLVED] Cannot find symbol error when trying to compile a random number generator

    I'm new to Java. Just trying to run a random number generator. The program is suppose to generate and print out random numbers between 0 and 9. The program is to stop when the number 7 is printed.

    I'm getting a bunch of "cannot find symbol" errors.

    Code:
    import java.util.Random;
    
    public class RandomGenerator{  //Begin class block
    
        public static void main (  String[] args ) {
    
    //Begin method block
    
            System.out.println("This is a Random Number Generator. It will print out 
    
    random numbers between 0 and 9 and will stop once the number 7 is generated");
    
            Random randNumber = new Random();
    
    int numSelected = randNumber.nextInt(10);
    
    System.out.printIn("Generated number:");
    System.out.printIn(numSelected);
    
    while (numSelected != 7) {
    	if (numSelected < 7)
    	 System.out.printIn ("It's less than 7");
    	else 
    	 System.out.printIn ("It's greater than 7");
    
    	}	
    if (numSelected == 7)
    	System.out.printIn("We have 7! We're done here.");
    	
    
        }
    }

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    Quote Originally Posted by MichiKen View Post

    Code:
    while (numSelected != 7) {
    	if (numSelected < 7)
    	 System.out.printIn ("It's less than 7");
    	else 
    	 System.out.printIn ("It's greater than 7");
    
    	}	
           if (numSelected == 7)
    	 System.out.printIn("We have 7! We're done here.");
    	
    
        }
    }
    I am not sure what your error is as you did not post your full exception, however your loop will run forever, you need to add this line
    numSelected = randNumber.nextInt(10);
    in the else part like this

    else
    System.out.printIn ("It's greater than 7");
    numSelected = randNumber.nextInt(10);
    }

    This way every time when generated num is not 7, it will regenerate a new number
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Apr 2008
    Posts
    136
    Thank you. Got it resolved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles