【问题标题】:Cannot find symbol System.console().readInt(); [duplicate]找不到符号 System.console().readInt(); [复制]
【发布时间】:2016-02-04 22:43:07
【问题描述】:

这是我的代码:

import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class Main {
public static void main(String[] args) throws FileNotFoundException {

    String enter = "Enter";
    String resolve = "Resolve";
    String store = "Store";

    // if file doesn't exist
    int nextjob = 1;
    int jobnumber = nextjob;
    int phonenumber = System.console().readInt();
    int numberoflines = System.console().readInt();
    String problem = System.console().readLine();
    int time = System.console().readInt();

    String command = System.console().readLine();
    if(command.equals(store)){
        PrintWriter writer = new PrintWriter("openjobs.txt");
        writer.println(nextjob);
        writer.println(jobnumber);
        writer.println(phonenumber);
        writer.println(numberoflines);
        writer.println(problem);
        writer.println(time);
        writer.close();
    }
  }
}

这是输出:

Main.java:14: error: cannot find symbol
        int phonenumber = System.console().readInt();
                                          ^
symbol:   method readInt()
location: class Console

【问题讨论】:

  • Console 类中没有 readInt() 方法。你想让我做什么?你想读取用户输入吗?
  • 有什么方法可以说 readInt() 吗?我猜没有。
  • 我想将用户输入读取为 int,例如 readLine()。
  • 我也考虑不使用System.console(),因为它可能会根据执行的环境返回 null。
  • 使用scanner

标签: java io


【解决方案1】:

使用ScannerScanner.nextInt() 方法仅将整数作为用户的输入。

Scanner sc = new Scanner(System.in);
int anyNumber = sc.nextInt();

如果用户输入的不是整数,它会抛出一个InputMismatchException

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2010-10-12
    • 2019-05-31
    相关资源
    最近更新 更多