Java bufferreader 在 ctrl z 上崩溃

标签 java

我正在制作一个游戏,直到用户在命令行中输入退出为止。

用户可以输入不同的命令,例如 get 和 go,使用 get 命令,用户可以说出想要什么,获取棒球棒。我在代码中所做的就是拆分命令。

一切工作正常,但我发现了一个无法解决的错误。如果我输入“get”并按 space 然后按 ctrl+z 它会进入一个永远不会结束的 while 循环。

只有 ctrl+z 才会发生(1 次使用 ctrl c 但之后 1 次没有不再)

 private void run() 
{       
    while (! quitCommand)
    {
        
        String input = null;
        
        try 
        {   
            input = null;
            System.out.println("Input "+ input);
            System.out.println("Give a command.");
            BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
            input = is.readLine();
            handleCommand(input);
            // As long as the command isn’t to quit:
            // get the next input line and handle it. (With handleCommand.)
        } 
        
        catch (Exception e) 
        {
            System.out.println("Something went wrong we are sorry try again.");
            e.printStackTrace();
        }
        
    }
}

/**
* @param userInput (This is the entire input string from the user.)
*
* (Tell others to) Perform the task which belongs to the given
* command.
*/
private void handleCommand(String userInput) 
{
    
    
    // Split the user input string.
    if (userInput != null)          // user input can not be empty
    {
        String[] delenTekst = userInput.split(" ");
        
        // The first word is a command. The rest is extra information
        String command = delenTekst[0];
        String extra = "";
        
        for (int i = 1; i < delenTekst.length; i ++)
        {
            if (i == 1)
            {
                extra = extra + delenTekst[i];
            }
            else
            {
                extra = extra +" " + delenTekst[i];
            }               
        }

        switch (command)
        {
            // Check if the command is to travel between rooms. If so, handle
            case "go"
            : 
                this.checkRoomTravel(extra);
                break;
            // If there isn't any room travel, then check all other command
            case "get"
            :   
                System.out.println("Looking for " +extra );
                this.handleGetCommand(extra);
                break;
            case "quit"
            :
                quitCommand = true;
                break;
            default
            :
                System.out.println("Command is not known try help for information");
                break;  
        }
    }
    else
    {
        userInput = "help";
    }
}

我是java新手,所以它可能非常简单。

在我的脚本顶部,我有一个私有(private) boolean 值quitCommand = false;,用于检查用户是否输入退出。

最佳答案

Ctrl+Z 关闭控制台,因此您的 readLine() 返回 null,假装表示已到达文件末尾。因此,您需要做的就是检查 readLine() 返回的 null 并在处理“退出”时处理它。

我更改了您的代码(只是为了测试我的论文),并且还简化了一些内容,例如您不需要在每次读取一行时重新创建一个 BufferedReader。

private boolean quitCommand = false;
 private void runIt() {       
     BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
     String input = null;

     while(!quitCommand) {
         try {   
             System.out.print("Give a command: ");
             input = is.readLine();

             // As long as the command isn’t to quit:
             if(input == null || "quit".equals(input.trim())) quitCommand = true;
             if(quitCommand) break;

             // get the next input line and handle it. (With handleCommand.)
             String[] words = input.trim().split("\\s+");

             // ** This is the original handleCommand line **
             System.out.println(input + ":" + Arrays.toString(words));
         } 
         catch (Exception e) {
             System.out.println("Something went wrong we are sorry try again.");
             e.printStackTrace();
         }
     }
 }

顺便说一句:要将输入拆分为单词,我将使用代码中所示的正则表达式。如果用户输入制表符或多个空格,这也适用。

关于Java bufferreader 在 ctrl z 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19467424/

相关文章:

java - 将 boolean 值转换为 boolean 值 (java)

java - Spring Security 无法在 Windows 7 下运行

java - CursorIndexOutOfBoundsException : Index 1 requested,,大小为 1。错误 - Android

java - 访问父类字段 "this.field"VS "super.field"

java - 如何用多个分隔符分隔字符串?

java - 许多 drawBitmap 后图像质量变差

java - 如何将Java中的Map对象保存到MySQL数据库中

java - EJB 全局异常处理程序

java - 此 URL 不支持 HTTP 方法 GET - Tomcat

java - 将 Jersey/Jackson 配置为不使用 @XmlElement 字段注释进行 JSON 字段命名