Error while Running Code
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
int counter = 0;
l1:
for (int i=0; i<10; i++) {
l2:
int j = 0; //compile error
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
when i try to run above code it gives error as follows
"not a statement int j = 0;"
i am not able to figure out why.
thanks
l1:
for (int i=0; i<10; i++) {
l2:
int j = 0; //compile error
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
when i try to run above code it gives error as follows
"not a statement int j = 0;"
i am not able to figure out why.
thanks
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Your topic says "Running the code", do you mean that - not Compiling the code?
This code (which should be enclosed in tags) is incomplete.
Is this all the code? No enclosing class? If you need to know what a Java program looks like, then I can transfer this topic over to our Beginners' Forum if you want.
[ July 22, 2006: Message edited by: Barry Gaunt ]
This code (which should be enclosed in tags) is incomplete.
Is this all the code? No enclosing class? If you need to know what a Java program looks like, then I can transfer this topic over to our Beginners' Forum if you want.
[ July 22, 2006: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand
Posts: 643
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ok here is complete code
public class Q275d {
public static void main(String[] args) {
int counter = 0;
l1:
for (int i=0; i<10; i++) {
l2:
int j = 0;//compile time error
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
}
}
There is compile time error as commented above saying
"not a statement int j = 0;"
i cant figure out why.
thanks
public class Q275d {
public static void main(String[] args) {
int counter = 0;
l1:
for (int i=0; i<10; i++) {
l2:
int j = 0;//compile time error
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
}
}
There is compile time error as commented above saying
"not a statement int j = 0;"
i cant figure out why.
thanks
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Indenting the code usually reveals such problems
Observe that the label l2 doesnot "label" any for or while loop. Probably what you meant was :
Now your code should compile fine.
[ July 22, 2006: Message edited by: Neelesh Bodas ]
Observe that the label l2 doesnot "label" any for or while loop. Probably what you meant was :
Now your code should compile fine.
[ July 22, 2006: Message edited by: Neelesh Bodas ]
posted 19 years ago
break Statement & Statement Labels
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Note the label does not have to be on the loop statement but does need to be on a statement, such as an if-else statement, that contains the break statement.
break Statement & Statement Labels
Gowher Naik
Ranch Hand
Posts: 643
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi Neelesh
does it mean that u cant insert any code between label and loop statement.
or in other words it means after label there must be loop statement only.
is it so?
does it mean that u cant insert any code between label and loop statement.
or in other words it means after label there must be loop statement only.
is it so?
Neelesh Bodas
Ranch Hand
Posts: 107
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Labels should be on the statement/construct that contains break or a continue statement. These include loops, if-else and switch statements.
Barry Gaunt
Ranch Hand
Posts: 7729
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It is also possible to write the following:
[ July 22, 2006: Message edited by: Barry Gaunt ]
[ July 22, 2006: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Neelesh Bodas
Ranch Hand
Posts: 107
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Interesting. I didn't know that. Thanks Barry.
| I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad? The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









