Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Dice Roller

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Dice Roller

    I am a very beginner (5 days) and I am trying to make a program that generates frequency on whatever sided dice you want. I dont want to know how to make it more efficient. Just what I did wrong. Here's the code:

    import java.util.Scanner;
    import java.util.Random;
     
    public class dieRoll 
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
    		System.out.println("Type number of sides on die: ");
    		int faces = input.nextInt();
    		int freq[] = new int[1 + faces];
    		for(int roll = 1; roll < 1000; roll++)
    		{
    			++freq[1 + rand.nextInt(faces)];
    		}
    		System.out.println("Face\tFrequency");
    		for(int side = 1; side < freq.length; side++);
    		{
    			System.out.println(side+ "\t" + freq[side]);
    		}
    	}
    }

    And here's the problem:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	side cannot be resolved to a variable
    	side cannot be resolved to a variable
     
    	at dieRoll.main(dieRoll.java:20)

    the problem is the last print line.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Dice Roller

    The error is telling you that the variable named 'side' is not a valid variable name on line 20 of the dieRoll class.
    Something must have caused the scope of the variable to end before you thought it would. Try looking on through the lines of code starting with where the variable was declared, moving down to where the variable is not able to be used. See what caused the variable's scope to end.

    The variable was declared as the index counter in your for loop, so the variable is good through the scope of the for loop.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Dice Roller

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	side cannot be resolved to a variable
    	side cannot be resolved to a variable
     
    	at dieRoll.main(dieRoll.java:20)

    the problem is the last print line.
    The problem is you're trying to run code that won't compile -- never do that. Instead compile your java file, find the compilation errors that are reported, and then fix them and compile again. Don't try to run the class until all compilation problems are fixed. If you're still stuck, you'll want to show us your complete compiler error message and indicate for us which lines are causing the errors. So, please show us, which line is line 20?

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dice Roller

    what I don't get is that I have this:

    import java.util.Random;
     
    public class diceRoll 
    {
    	public static void main(String[] args)
    	{
    		Random rand = new Random();
    		int freq[] = new int[7];
    		for(int roll = 1; roll < 1000; roll++)
    		{
    			++freq[1 + rand.nextInt(6)];
    		}
    		System.out.println("Face\tFrequency");
    		for(int face = 1; face < freq.length; face++)
    		{
    			System.out.println(face + "\t" + freq[face]);
    		}
    	}
    }

    which seems to me to be exactly the same but a little bit simpler and it still works.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dice Roller

    line 20 is

    System.out.println(side+ "\t" + freq[side]);

    it is the last println

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Dice Roller

    The hint given earlier is that the variable is visible through the scope of the for loop. The problem is not the variable, the problem is the scope of the for loop has been cut short. Compare the lines of code you used to see why.

    for(int face = 1; face < freq.length; face++)
    is not exactly like
    for(int side = 1; side < freq.length; side++);

  7. The Following User Says Thank You to jps For This Useful Post:

    astraya008 (October 8th, 2012)

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dice Roller

    OMG I feel like such an idiot! I have been stuck on this for so long. Thank you so much!

Similar Threads

  1. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 03:25 AM
  2. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM
  3. [SOLVED] Help printing random dice
    By vanDarg in forum Java Theory & Questions
    Replies: 12
    Last Post: February 1st, 2011, 03:09 PM
  4. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 08:50 AM

Tags for this Thread