【问题标题】:Validating user input in java在java中验证用户输入
【发布时间】:2016-03-15 00:40:51
【问题描述】:

您好,我正在处理的一个 uni 项目遇到问题。我正在尝试验证输入,以便在尝试借书时输入的BookID 仅在它存在于名为“BookList”的数组中时才有效。在我让它工作的那一刻,它会验证它以确保输入一个整数,而不是字母或负数。

我尝试了无数次,但我完全被卡住了??任何提示或帮助,我将不胜感激。 谢谢

//loan a book method
            public void loanBook() {
                int loanID;
                do {
                    System.out.println("Please enter the Book ID of the book that you wish to borrow");
                    while (!input.hasNextInt()) { // checking that the ID entered is an integer - validation
                        System.out.println("That is not an integer");
                        input.nextLine(); //pushing the scanner on
                    }
                    loanID = input.nextInt(); //setting the loanID variable equal to the input from the scanner.
                }
                while (loanID < 0 || loanID > 100000000); //VALIDATION - NEED TO CHANGE SO THAT WHILE LOAN ID EXISTS IN ARRAY LIST ????
                for (int i = 0; i < BookList.size(); i++) { //for loop to go through and check for the ID entered to remove the book that it corresponds to
                    if (BookList.get(i).getBookID() == loanID ) {
                        System.out.println("The book named : " + BookList.get(i).getTitle() + " has now been taken out on loan. Please return within 2 weeks!");
                        BookList.get(i).setStatus("On Loan");;
                    }//end of if statement

                }//end of for loop

            } //end of return book method

【问题讨论】:

  • 那是什么问题?
  • 在第 11 行,我目前正在验证输入的数字必须在 0 到 100000000 之间。我需要更改它,以便输入的数字必须是我的数组列表中名为 BookList 的元素,所以我可能会产生错误提示“图书馆中不存在这本书。”
  • 直接使用 input.NextInt() 并使用 InputMismatchException 保护块。当这个异常被捕获时,您输入了除整数之外的任何其他内容。如果没有抛出,你现在有你的整数。随时随地使用它来遍历您的书单 ID

标签: java validation validate-request


【解决方案1】:

您可以对 Arraylists 使用 .contains() 方法。您只需要确保根据它们的状态删除项目。

if(bookList.contains(loanID)){
   //logic for book exists
}else{
   //book is on loan.
}

现在,正如我所说,您需要确保进行适当的验证,以移除借出的书籍等,这样才能正常工作。您现在拥有逻辑的方式是对循环进行大量不必要的工作。这样您就可以轻松扫描列表并找到所需的项目。当然有更好的方法来设置你的列表等,但这应该可以让你的代码非常相似。

编辑

您要求提供有关在确认项目存在后如何找到该项目索引的信息。这仍然很简单。一旦您确认该项目存在,您将使用以下行:

int index = bookList.indexOf(loanID);

这将在您的 ArrayList 中返回该书所在位置的索引。一旦你有了索引,你就可以开始做你以前做的所有事情了:

bookList.get(index).getBookId();

bookList.get(bookList.indexOf(itemId)).getBookId();

这几乎与您之前所做的完全相同,但减少到 3 行并且可以更短。

if (BookList.contains(loanID)) {
     int index = BookList.indexOf(loanId);
     if (!BookList.get(index).getStatus().equals("On Loan")) {
         System.out.println("The book named: " + BookList.get(index).getTitle() + " has now been taken on loan.");
         BookList.get(index).setStatus("On Loan.");
     }else{
         System.out.println("Book is on loan already.");
     }
}else{
    //logic for not existing.
}

【讨论】:

  • 那么您将如何操作您发现存在于数组列表中的书?即使用与它相关的变异器,因为在mintue我正在使用一个循环,所以我可以使用get(i)来操纵它。谢谢
  • 从字面上看就在您的初始验证下方,但我不想为您编写所有代码。试着先把它扔进去,然后看看会发生什么:) 这就是编码的乐趣。
【解决方案2】:

创建一个变量 int isExist = 0;从用户那里获得输入后……通过数组查看那本书是否存在。然后使 isExist=1;然后循环只做 if 语句

if( isExist == 0) { 
System.out.println("Book is not found");
}

顺便说一句,一旦你在数组中找到了你想使用break;跳出循环的书

【讨论】:

  • 既然可以使用布尔值,为什么还要使用int
猜你喜欢
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多