3

在做一个学校项目时,我遇到了以下问题:在processCommand()run()方法中向方法中输入一行之后(使用控制台作为一种文本编辑器),我发现无论出于何种原因,令牌都无法正确读取因此,processCommand()即使命令应该简单而准确(即在控制台中键入 h 会显示“请输入有效命令。”),我也会运行该方法的“else”子句。我确定我如何拆分线路有问题,但对于我的生活,我无法弄清楚。谁能帮我?

    @Override
public void run() {
    Scanner scanner = new Scanner(System.in);
    String line;

    try {
        do {
            System.out.print("vi: ");
            line = scanner.nextLine();
            if(line.length() > 0){
                processCommand(line);
            }
        } while (!quit);
    } catch (Exception ex) {
        System.out.println("An error occurred.");
    }

    scanner.close();
    System.out.println("Goodbye!");
}

@Override
public void processCommand(String line) {
    String[] tokens = line.split(" ");
    if(tokens[0] == "b") {
        insertBefore(line.substring(3));
    }
    if(tokens[0] == "i") {
        insertAfter(line.substring(3));
    }
    if(tokens[0] == "m") {
        moveUp(Integer.parseInt(line.substring(3)));
    }
    if(tokens[0] == "u") {
        moveDown(Integer.parseInt(line.substring(3)));
    }
    if(tokens[0] == "r") {
        remove(Integer.parseInt(line.substring(3)));
    }
    if(tokens[0] == "d") {
        remove(Integer.parseInt(line.substring(3)));
    }
    if(tokens[0] == "c") {
        clear();
    }
    if(tokens[0] == "s") {
        try {
            save(line.substring(3));
        } catch (IOException e) {
        }
    }
    if(tokens[0] == "l") {
        try{
            load(line.substring(3));
        } catch (IOException e) {

        }
    }
    if(tokens[0] == "h") {
        displayHelp();
    }
    if(tokens[0] == "x") {
        exit();
    }
    else {
        System.out.println("Please enter a valid command.");
        return;
    }
}
4

2 回答 2

3

您不应该使用“==”进行字符串比较。使用以下内容:

if(tokens[0].equals("h"))...

希望有帮助!

于 2012-12-04T00:53:41.240 回答
0

仅供参考,这是使用 switch 语句的替代版本(可能更易于阅读或键入的字符更少):

public void processCommand(String line) {
    String[] tokens = line.split(" ");
    if(tokens[0].length() == 0) {
        System.out.println("Please enter a valid command.");
        return;
    }
    switch(tokens[0].charAt(0)) {
        case 'b':  insertBefore(line.substring(3));
                   break;
        case 'i':  insertAfter(line.substring(3));
                   break;
        case 'm':  moveUp(Integer.parseInt(line.substring(3)));
                   break;
        case 'u':  moveDown(Integer.parseInt(line.substring(3)));
                   break;
        case 'r':
        case 'd':  remove(Integer.parseInt(line.substring(3)));
                   break;
        case 'c':  clear();
                   break;
        case 's':  try {
                       save(line.substring(3));
                   } catch (IOException e) { }
                   break;
        case 'l':  try {
                       load(line.substring(3));
                   } catch (IOException e) { }
                   break;
        case 'h':  displayHelp();
                   break;
        case 'x':  exit();
                   break;
        default:   System.out.println("Please enter a valid command.");
    }
}
于 2012-12-04T01:05:16.227 回答