简体   繁体   中英

println cannot be resolved as a variable

I am studying object oriented programming in my university and my professors is making an example of streams on java. Unfortunately when I try to run this code it says "println cannot be resolved as a variable" on System.out::println . It also says to delete the two colons, while on my professor's eclipse it does work.

This is the code. I am running it on eclipse for ubuntu and my version of java is 1.8 update 91

package stream;

import java.util.Arrays;

public class StreamExamples {

    public static void main(String[] args){
        String text = "I have no idea what is the problem";
        String[] words = text.split(" ");

        Arrays.stream(words)        
        .sorted()
        .limit(3)
        .forEach(System.out::println);
    }
}

Most likely the version of Eclipse you are running does not understand Java 8 syntax.

You need to be running the current Eclipse 4.6 (Neon) or the previous release 4.5 (Mars) for full Java 8 support. You code works fine on Eclipse Neon.

1) Check your build path by right clicking on your project, Build Path > Configure Build Path . The libraries tab should show a JRE with version 1.8, if that is not the case, click Edit and select a JRE that is at least version 8.

2) Check the compiler settings by right clicking on your project, Properties > Java Compiler . Change the Compiler compliance level to 1.8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM