原创

Java 程序中控制台输入延迟的问题

温馨提示:
本文最后更新于 2024年04月12日,已超过 47 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I'm working on a Java program that takes user input from the console using BufferedReader for a command-line interface. However, I'm experiencing a noticeable delay when printing the prompt ">" after the user presses Enter.

take the windows CMD or Cisco CLI for example, when you press enter it immediately moves to the next line. However, in this program it instantly moves to the next line BUT it takes a fairly considerable delay before it reprints the "> ".

Say if i press enter 6 times in quick concession:

What I want:

    > 
    > 
    > 
    > 
    > 
    > 

What I get: 
\n
\n
\n
\n
\n
    > > > > > > 

I'm in no means an experienced programmer, this is for a second-year solo uni project. But I would love to find out how this could be optimized, or if its simply my IDE hahaha, we have to use netbeans.

Heres the relevant snippet that reprints "> ":

public static void main(String[] args) {
        Printer tp = new TaskPrinter();
        TaskCreator tc = new TaskCreator();
        TaskRemover tr = new TaskRemover();
        TaskUpdater tu = new TaskUpdater();
        TaskManagerController tmc = new TaskManagerController(tp, tc, tr, tu);

        try ( BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.println("Enter '?' to see list of commands.");
            System.out.println("Enter 'exit' to end session.");
            System.out.println("Max Name Length: " + tc.getNameLength());
            String userInput;
            do {

**//"> " IS REPRINTED HERE**
                System.out.print("> ");
                System.out.flush();
                userInput = reader.readLine().trim();

**// THIS METHOD IS RUN BEFORE GOING BACK TO PRINT "> " AGAIN.
// NOT SURE IF THIS IS THE PROBLEM OR NOT :)**
                tmc.processCommand(userInput);
            } while (!userInput.equalsIgnoreCase("exit"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

If its relevant here is the processCommand method:

public void processCommand(String userInput) {
        userInput = userInput.toLowerCase();
        switch (userInput) {
            //check for commands
            case "":
                break;
            case "display all":
            case "display a":
            case "display al":
                tp.printDisplayAll();
                break;
            case "display task":
            case "display t":
            case "display ta":
            case "display tas":
                tp.printDisplay();
                break;
            case "remove all":
            case "remove a":
            case "remove al":
                tr.removeAll();
                break;
            case "remove task":
            case "remove t":
            case "remove ta":
            case "remove tas":
                tr.removeTask();
                break;
            case "create":
            case "c":
            case "cr":
            case "cre":
            case "crea":
            case "creat":
                tc.createTask();
                break;
            case "?":
                tp.printCommands();
                break;
            case "update":
            case "u":
            case "up":
            case "upd":
            case "upda":
            case "updat":
                tu.updateCompleteStatus();
                break;
            case "remove":
            case "r":
            case "re":
            case "rem":
            case "remo":
            case "remov":
                System.out.println("unambiguous command");
                break;
            case "display":
            case "d":
            case "di":
            case "dis":
            case "disp":
            case "displ":
            case "displa":
                System.out.println("unambiguous command");
                break;
            default:
                System.out.println("Unrecognized Command.");
                break;
        }
    }

It was originally scanning input with the Scanner class, I was hoping the buffered reader would make a difference but unfortunately not. the switch statement in the processCommand method was also originally an if else ladder. I changed it to a switch statement as I always thought these were quicker, but the delay is unchanged.

Any kind of help would be greatly appreciated and I'm very open to trying any ideas suggested. Thanks everyone.

I've noticed that the problem is being fixed by using System.out.println("> "); instead of System.out.print("> "); However I want to use System.out.print() as I do not want the extra newline

as another note, If I use Eclipse there is no delay in printing. As Eclipse is forbidden by our lecturer, it looks like I may be doomed with using Netbeans!!

正文到此结束
热门推荐
本文目录