簡體   English   中英

Java 緩沖區讀取器在 ctrl z 上崩潰

[英]Java bufferreader crashes on ctrl z

我正在制作一個游戲,直到用戶在命令行中輸入退出為止。

用戶可以輸入不同的命令,如 get 和 go,使用 get 命令,用戶可以說要得到什么,得到棒球棒。 我在代碼中所做的是拆分命令。

一切正常,但我發現了一個我無法解決的錯誤。 如果我輸入“get”並按空格然后ctrl + z它會進入一個永遠不會結束的 while 循環。

它只在ctrl + z時發生(用ctrl c 1 次,但之后 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 新手,所以它可能非常簡單。

在我的腳本頂部,我有一個私有的布爾值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();
         }
     }
 }

順便說一句:要將輸入拆分為單詞,我將使用正則表達式,如我的代碼所示。 如果用戶輸入制表符或多個空格,這也適用。

在 DOS/Windows 上Ctrl + Z表示輸入結束。 無論您調用多少次,這都會導致 readLine() 返回null 這很可能會導致您的代碼失敗,因為您似乎沒有檢查它。 我懷疑你得到了一個 NullPointerException 你假裝沒有發生並無休止地重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM