【问题标题】:System.out.println() and BufferedReader mixing up output in consoleSystem.out.println() 和 BufferedReader 在控制台中混合输出
【发布时间】:2013-05-02 11:48:21
【问题描述】:

我目前正在开发一个程序,该程序使用BufferedReader 读取输入,System.out.println() 用于输出。

这是我的代码:

public void chooseMethod() throws IOException{
    int in = 0;
    while(true){

        System.out.println("What do you want to do? (0 to exit, 1 to read Bank         Account, 2 to write Bank Account, 3 to read Bill, 4 to write Bill): ");

        in = Integer.parseInt(cin.readLine());

        if(in == 0){
            break;
        }else if((in < 0) || (in > 4)){
            System.out.println("Invalid choice.");
        }else if(in == 1){
            showBankAccount();
        }else if(in == 2){
            insertBankAccount();
        }else if(in == 3){
            showBill();
        }else if(in == 4){
            insertBill();
        }

    }

    dbm.close();

}

public void insertBankAccount() throws IOException{
    int bankNr = 0;
    int sortCode = 0;
    int accountNumber = 0;
    int balance = 0;
    int interest = 0;
    String details;
    String name;

    while(true){
        System.out.println("Enter bankNr (0 to exit): ");
        bankNr = Integer.parseInt(cin.readLine());

        if(bankNr == 0){
            break;   

        }else if(bankNr <= maxBankNr){
            System.out.println("Invalid Number: already taken");
            break;
        }

        System.out.println("Enter Account Name: ");
        name = cin.readLine();

        System.out.println("Enter Sort Code: ");
        sortCode = Integer.parseInt(cin.readLine());

        System.out.println("Enter Account Number: ");
        accountNumber = Integer.parseInt(cin.readLine());

        System.out.println("Enter balance: ");
        balance = Integer.parseInt(cin.readLine());

        System.out.println("Enter Interest: ");
        interest = Integer.parseInt(cin.readLine());

        System.out.println("Enter Details: ");
        details = cin.readLine();

        bankAccountDAO.insertBankAccount(bankNr, sortCode, accountNumber, balance, interest, details, name);
        this.maxBankNr = bankAccountDAO.getMaxBankNr();


    }


}

现在,假设我输入“2”来“写银行账户”,输出如下:

运行:

What do you want to do? (0 to exit, 1 to read Bank Account, 2 to write Bank Account, 3 to read Bill, 4 to write Bill):

Enter bankNr (0 to exit): 

2

输出中的这个“2”应该在“Enter bankNr(0 to exit):”之前

我曾多次遇到过这个问题,ScannerBufferedReader 都遇到过这个问题,并且在谷歌上搜索了很长时间,但似乎找不到任何答案。

还有其他人遇到过这个问题吗?如果是这样,您是如何解决的?

【问题讨论】:

    标签: java io bufferedreader


    【解决方案1】:

    在你的 insertBankAccount() 方法里面 while 循环改变:

    System.out.println("Enter bankNr (0 to exit): ");
    bankNr = Integer.parseInt(cin.readLine());
    

    收件人:

    bankNr = Integer.parseInt(cin.readLine());
    System.out.println("Enter bankNr (0 to exit): ");
    

    【讨论】:

    • 谢谢 :) 这行得通。我相信这会在未来为我节省几个小时:)
    • 在编写提示之前接受输入没有任何意义。
    • 我认为这也没有任何意义,但它确实有效:S 我想知道为什么它会倒退......
    • 我怀疑它可能会阻塞下一个 int。
    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    相关资源
    最近更新 更多