Results 1 to 9 of 9
  1. #1
    pablo2002 is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default Java Compile error

    Have a variable error on this module. I cannot fiqure out why.

    Java Code:
    System.out.print("set #" + k + " = ");
    for(int i=0; i <= 6; i++)
    System.out.print(NoDuplicateNumbers[i] + ", ");
    The rest of code applet. Just need a hint, please.

    Java Code:
    public class Week3IPSolutionSkeleton {
    
    	
    public static void main(String [] args) {
    
    
    			
    int[] NoDuplicateNumbers = {0,0,0,0,0,0,0};
    			
    int IndexOfNoDuplicateNumbers=0;
    			
    boolean match = false;
    
    
    
    				
    for (int k=1; k <= 8; k++)
    
    				// Keep doing till you get 7 no duplicate numbers
    				while (IndexOfNoDuplicateNumbers < 7)
    				{
    					int Number = ((int) (35 * Math.random() ) + 1);
    					System.out.println(Number);
    
    					for (int i=0; i < IndexOfNoDuplicateNumbers; i++)
    						if ( Number == NoDuplicateNumbers[i] )
    						{
    							match = true;
    							break;
    						}
    
    					if (match == true)
    					{
    						match = false;
    					}
    					else
    					{
    						NoDuplicateNumbers[IndexOfNoDuplicateNumbers] = Number;
    						IndexOfNoDuplicateNumbers++;
    					}
    
    				}
    
    				//Print 7 and no duplicate numbers for the rest
    				System.out.print("set #" + k + " = ");
    				for(int i=0; i <= 6; i++)
    				System.out.print(NoDuplicateNumbers[i] + ", ");
    
    
    
    				// Print the powerball number
    				System.out.print( (int) (10 * Math.random() ) + 1);
    
    
    				System.out.println();
    				System.out.println();
    
    
    				// initialize for next set of 7 numbers
    				for (int i=0; i<7; i++)
    					NoDuplicateNumbers[i] = 0;
    				IndexOfNoDuplicateNumbers=0;
    				match = false;
    
    
    		}
    
    }
    Last edited by Fubarable; 09-11-2010 at 08:00 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum! I've edited your post above to add code tags so that the code will retain its formatting.

    Can you tell us what the error is and which line causes it?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Suggestion: place all for loops, if blocks, while loops, and similar within curly braces even if only one line. For instance instead of this:
    Java Code:
          for (int i = 0; i <= 6; i++)
             System.out.print(NoDuplicateNumbers[i] + ", ");

    do this:
    Java Code:
          for (int i = 0; i <= 6; i++) {
             System.out.print(NoDuplicateNumbers[i] + ", ");
          }

    This will save your tail many times, and may even help you solve your current problem. ;)

  4. #4
    pablo2002 is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    The error is on this module.

    /Print 7 and no duplicate numbers for the rest
    System.out.print("set #" + k + " = ");
    for(int i=0; i <= 6; i++)
    System.out.print(NoDuplicateNumbers[i] + ", ");

    Line 40
    Cannot find symbol k
    System.out.print("set #" + k + " = ");

    Their is mark on "K"

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You have a scope issue -- a variable that you're trying to use is not in scope; it is visible within the for loop but not before the loop nor after it. If you take my suggestion above regarding the curly braces, you'll know better if the variable "k" is in the same scope as the k variable declared in the for loop.

  6. #6
    pablo2002 is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    I did add the brackets. Re compiled and received the same error. The variable is delcared and is with scope of java app. If I declare it again, it will casue another error.

    It is declared here "for (int k=1; k <= 8; k++)"

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pablo2002 View Post
    I did add the brackets. Re compiled and received the same error. The variable is delcared and is with scope of java app. If I declare it again, it will casue another error.

    It is declared here "for (int k=1; k <= 8; k++)"
    Yep, it is declared there, and thus it is only visible within the for loop itself:

    Java Code:
    // k is not visible here as it is out of scope
    
    for (int k=1; k <= 8; k++) {
       
       // here inside of the for loop, k is in scope and is visible
    
    } // end of for loop block
    
    // k is not visible here as it is again out of scope
    Hopefully the curly braces and comments above will help you see where k is in scope.

  8. #8
    pablo2002 is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    You were correct! I changed and put my curly brackets in the correct spots and it worked. Thank you for your help most sincerely! :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pablo2002 View Post
    You were correct! I changed and put my curly brackets in the correct spots and it worked. Thank you for your help most sincerely! :)
    Wonderful, and you're welcome, most sincerely! :)

Similar Threads

  1. Compile Error
    By gcorreageek in forum Advanced Java
    Replies: 2
    Last Post: 09-08-2010, 05:23 AM
  2. compile error
    By angryredantz in forum New To Java
    Replies: 1
    Last Post: 01-23-2009, 10:44 PM
  3. Compile Error - Please Help!!
    By AJ2009 in forum New To Java
    Replies: 10
    Last Post: 01-04-2009, 03:59 PM
  4. Java 1.5 compile time error
    By ank_k in forum New To Java
    Replies: 4
    Last Post: 11-13-2008, 11:12 AM
  5. compile error
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-12-2007, 06:00 PM

Posting Permissions

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