0

Below is just an example of a java program using console operator and the program should be able to compile fine, but for some reason, my eclipse is not able to read "Console" operator? please Help!.

public class Dot_operator {
public static void main (String [] args){
 String name3 = Console.readLine("hello");
        System.out.println(name3);
 }
}
4

2 回答 2

1

Console#readLine() is an instance method. You would need an instance of Console to invoke it.

Console console = System.console();
String name3 = console.readLine("hello");
System.out.println(name3);

Of course, you would need to import java.io.Console. Note that, this will thrown a NPE when run on eclipse, as System.console() will give you null. You should better use Scanner class here.

于 2013-10-11T22:07:31.350 回答
0

You probably made a simple typo: you didn't close the bracket next to the args:

public class Dot_operator {
public static void main (String [] args) {
   Scanner sc = new Scanner(System.in);
   System.out.println("hello");
   String name3 = sc.nextLine();
   System.out.println(name3);
 }
}

This works on my machine.

于 2013-10-11T22:07:56.800 回答