当用户输入字符串时,按Enter键没有任何反应

时间:2015-01-03 17:52:33

标签: java enter

我正在开发一个图书馆应用程序,允许用户存储,借阅和返回技术手册。

在构建应用程序的返回部分时遇到了问题。

目前,如果用户借用手册并希望退回手册,则必须从显示的借阅手册列表中输入手册的标题。但是,如果用户希望在该列表中的第一个之后返回任何手册,则不会发生任何操作。用户输入名称并按Enter键,但应用程序只是暂停。

以下是我所描述的错误示例:

enter image description here

以下是相关代码:

static void returnManual(){
    System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");
    if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && borrowedManuals.size() >= ManualChoice){
        for (int i = 0; i < borrowedManuals.size(); i++)
            System.out.println(borrowedManuals.get(i).displayManual());
        returnManualTitle = Console.readString(Messages.enterManualTitle, Messages.tooShortMessage, 3);
    }

    int x = 0;
    boolean titleExistance = false;
    while (x < ManualList.size()){//Search for the Manual by title, if it exists change it's status,
                                //it's borrower and borrowDate.

        if (ManualList.get(x).title.equalsIgnoreCase(returnManualTitle)){

            ManualList.get(x).status = "Available";
            ManualList.get(x).borrower = "N/A";
            ManualList.get(x).borrowDate = "N/A";
            ManualList.get(x).returnDate = "N/A";

            int p = 0;
            borrowLoop:
            while (p < borrowedManuals.size()){//Search for the Manual by title, if it exists change it's status,
                //it's borrower and borrowDate.

                if (borrowedManuals.get(p).title.equalsIgnoreCase(returnManualTitle)){

                    borrowedManuals.remove(p);
                    break borrowLoop;
                }

            }               
            System.out.println(Messages.successReturnMessage);
            titleExistance = true;

            break;//if a title is found, break out of the loop and display choice menu.
        }
        x = x+1;
    }//end of while loop.
    if(titleExistance == false){
        boolean repeatReturnManual = Console.readYesNo("\n--------------------------------------------------------------------------" + "\n\nThe Manual with the title "+"\""+returnManualTitle +"\""+ " wasn't found!"
                                                        +"\n\nDo you want to try again? (Y/N):\n");
        System.out.println("\n--------------------------------------------------------------------------");
        if(repeatReturnManual){
            returnManual();
        }else{
            Menu.displayMenu();
        }
    }else if(titleExistance){
        Menu.menuChoice = 7;
    }               
}

/**
 * Removes the Manual.
 */

2 个答案:

答案 0 :(得分:1)

借用手动while循环中的p需要递增,否则它将以无尽的lopp运行。

while (p < borrowedManuals.size()) {
    Manual borrowed = borrowedManuals.get(p); // guessing the name of this class
    if (borrowed.title.equalsIgnoreCase(returnManualTitle)) {
        borrowedManuals.remove(p);
        break;
    }
    p++; // this is mising
}        

(我不确定整个线性搜索业务是否如此优秀,但我们不想重写整个应用程序,对吧?:)

答案 1 :(得分:0)

您的方法没有正确的返回类型。将返回类型更改为String,并将具有Console.readString()的行放在for循环之外。希望有所帮助!