touchdown
touchdown

Reputation: 21

';' expected and illegal start of expression error

I'm java programming in linux but it reports the following error. Can someone help me out? Thanks in anticipation!

Break.java:4: ';' expected  
    for (int x:numbers) {  
              ^  
Break.java:11: illegal start of expression  
  }  
  ^  
2 errors

 

public class Break {
    public static void main(String args[]) {
        int [] numbers={10,20,30,40,50};
        for (int x:numbers){
            if (x==30){
                break;
            }
            System.out.print(x);
            System.out.print("\n");
        }
    }
}

Upvotes: 0

Views: 609

Answers (1)

rgettman
rgettman

Reputation: 178313

You must be using JDK 1.4 or before. Your code compiles in 1.5, but not in 1.4:

$ javac Break.java
$ javac -source 1.4 -target 1.4 Break.java
Break.java:4: for-each loops are not supported in -source 1.4
(try -source 1.5 to enable for-each loops)
    for (int x:numbers){
              ^
1 error

You must use Java 1.5+ to use the foreach loop syntax, which was introduced in Java 1.5.

Upvotes: 9

Related Questions