找不到符号方法console();

时间:2012-05-19 16:04:15

标签: java console

这是我的代码:---

import java.lang.*;
class Console
{
        public static void main(String args[])
        {
            char i;
            i=System.console().readLine("this is how we give he input to the string");
            System.out.println("this is what we want to print:0)");
            System.out.println(i);

        }
}

我得到的输出是: -

输出: -

.java:7: cannot find symbol
symbol  : method console()
location: class java.lang.System
    i=System.console().readLine("this is how we give he input to the string");
                ^
1 error

Tool completed with exit code 1

如果有人可以帮助我......

2 个答案:

答案 0 :(得分:0)

使用jdk版本时出错,因为它必须是jdk1.6或更高版本,并且当更改为更新的jdk时, 有编译问题,System.console().readLine()会返回String,但您已分配char

答案 1 :(得分:0)

此外,某些IDE在控制台类中遇到问题(可能是因为他们自己使用它将输出重定向到窗口/对话框)

所以一个非常好的解决方法是使用:

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readline();
    //or if you want a char
    char i = str.charAt(0);

希望有所帮助