Results 1 to 9 of 9
Thread: Java Compile error
- 09-11-2010, 07:58 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Java Compile error
Have a variable error on this module. I cannot fiqure out why.
The rest of code applet. Just need a hint, please.Java Code:System.out.print("set #" + k + " = "); for(int i=0; i <= 6; i++) System.out.print(NoDuplicateNumbers[i] + ", ");
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
-
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?
-
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. ;)
- 09-11-2010, 10:12 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
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"
-
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.
- 09-11-2010, 11:23 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
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:
Hopefully the curly braces and comments above will help you see where k is in scope.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
- 09-12-2010, 12:08 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
You were correct! I changed and put my curly brackets in the correct spots and it worked. Thank you for your help most sincerely! :)
-
Similar Threads
-
Compile Error
By gcorreageek in forum Advanced JavaReplies: 2Last Post: 09-08-2010, 05:23 AM -
compile error
By angryredantz in forum New To JavaReplies: 1Last Post: 01-23-2009, 10:44 PM -
Compile Error - Please Help!!
By AJ2009 in forum New To JavaReplies: 10Last Post: 01-04-2009, 03:59 PM -
Java 1.5 compile time error
By ank_k in forum New To JavaReplies: 4Last Post: 11-13-2008, 11:12 AM -
compile error
By dirtycash in forum New To JavaReplies: 6Last Post: 12-12-2007, 06:00 PM


LinkBack URL
About LinkBacks


Bookmarks