Scanner 和 While 循环问题以检查数组列表
Issue with Scanner and While loop to check an arraylist
通常情况下,我可以毫无问题地扫描数组列表中的某些元素。我知道如何构造 while 循环等。但是,在这种情况下,我需要使用扫描仪,但它给我的问题如下所示:
以下代码旨在使用扫描仪输入作者和书名,以检查数组列表中是否有确切的书(由作者和书名的精确匹配组成)。
很可能我忽略了一些简单的事情,但无论如何,我不需要任何评论来评论这是一个愚蠢的代码等等。
public String checkForBookUsingInfo(){
int index = 0;
Book bookObject = null;
String returnValue = "Book not found";
String title = "";
String author = "";
Boolean isFound = false;
while (index <bookList.size() && isFound == false ){
bookObject = bookList.get(index);
System.out.println("Please enter title of book to search for.");
String anyTitle = keybd.next();
System.out.println("Please enter author of book to search for.");
String anyAuthor = keybd.next();
if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
returnValue = "Book is in library.";
}
index++;
}
return returnValue;
next()
return只有一个标记(单词),因此对于像 The Prince
这样的数据,第一个 next()
将 return "The"
第二个next()
将 return "Prince"
(因此它不会等待用户输入,因为它已经有了它的令牌)。
如果您想阅读更多内容,请用 nextLine()
一个字阅读整行。
如果您想在代码中同时使用 next()
和 nextLine()
,您应该阅读 Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
还有一些其他问题:
- 您没有将
isFound
设置为 true
是否会找到书;
- 你每次迭代书籍时都在询问书名和作者,但你应该在迭代之前知道这些信息,所以也许让用户将这些信息作为方法参数传递。
- 您正在将用户提供的值与来自
title
和 author
字段的空字符串 (""
) 进行比较
您的代码应该更像:
class Book{
private String author;
private String title;
//getters and setters
}
class Library {
private List<Book> bookList = new ArrayList<Book>();
public String checkForBookUsingInfo(String author, String title){
for (Book book : bookList){
if (book.getAuthor().equals(author) && book.getTitle().equals(title)){
return "Book is in library.";
}
}
return "Book not found in library";
}
public static void main(String[] args) throws Exception {
Scanner keybd = new Scanner(System.in);
Library library = new Library();
//add some books to library
//....
System.out.println("Please enter title of book to search for.");
String anyTitle = keybd.nextLine();
System.out.println("Please enter author of book to search for.");
String anyAuthor = keybd.nextLine();
String stateOfBook = library.checkForBookUsingInfo(anyAuthor, anyTitle);
System.out.println(stateOfBook);
}
}
事实证明,另一个答案让我开始思考(我今天还没有这样做)。
显然我在我想要的东西的正确轨道上,我只需要重新安排东西。尽管下面的代码有效,但另一种方法更容易阅读。
public String checkForBookUsingInfo(){
int index = 0;
Book bookObject = null;
String returnValue = "Book not found";
String title = "";
String author = "";
Boolean isFound = false;
System.out.println("Please enter the name of a book to search for.");
String anyTitle = keybd.nextLine();
System.out.println("Please enter the name of an author to search for.");
String anyAuthor = keybd.nextLine();
while (index <bookList.size() && isFound == false ){
bookObject = bookList.get(index);
title = bookObject.getTitle();
author = bookObject.getAuthor();
if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
returnValue = "Book is in library.";
}
index++;
}
return returnValue;
}
通常情况下,我可以毫无问题地扫描数组列表中的某些元素。我知道如何构造 while 循环等。但是,在这种情况下,我需要使用扫描仪,但它给我的问题如下所示:
以下代码旨在使用扫描仪输入作者和书名,以检查数组列表中是否有确切的书(由作者和书名的精确匹配组成)。
很可能我忽略了一些简单的事情,但无论如何,我不需要任何评论来评论这是一个愚蠢的代码等等。
public String checkForBookUsingInfo(){
int index = 0;
Book bookObject = null;
String returnValue = "Book not found";
String title = "";
String author = "";
Boolean isFound = false;
while (index <bookList.size() && isFound == false ){
bookObject = bookList.get(index);
System.out.println("Please enter title of book to search for.");
String anyTitle = keybd.next();
System.out.println("Please enter author of book to search for.");
String anyAuthor = keybd.next();
if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
returnValue = "Book is in library.";
}
index++;
}
return returnValue;
next()
return只有一个标记(单词),因此对于像 The Prince
这样的数据,第一个 next()
将 return "The"
第二个next()
将 return "Prince"
(因此它不会等待用户输入,因为它已经有了它的令牌)。
如果您想阅读更多内容,请用 nextLine()
一个字阅读整行。
如果您想在代码中同时使用 next()
和 nextLine()
,您应该阅读 Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
还有一些其他问题:
- 您没有将
isFound
设置为true
是否会找到书; - 你每次迭代书籍时都在询问书名和作者,但你应该在迭代之前知道这些信息,所以也许让用户将这些信息作为方法参数传递。
- 您正在将用户提供的值与来自
title
和author
字段的空字符串 (""
) 进行比较
您的代码应该更像:
class Book{
private String author;
private String title;
//getters and setters
}
class Library {
private List<Book> bookList = new ArrayList<Book>();
public String checkForBookUsingInfo(String author, String title){
for (Book book : bookList){
if (book.getAuthor().equals(author) && book.getTitle().equals(title)){
return "Book is in library.";
}
}
return "Book not found in library";
}
public static void main(String[] args) throws Exception {
Scanner keybd = new Scanner(System.in);
Library library = new Library();
//add some books to library
//....
System.out.println("Please enter title of book to search for.");
String anyTitle = keybd.nextLine();
System.out.println("Please enter author of book to search for.");
String anyAuthor = keybd.nextLine();
String stateOfBook = library.checkForBookUsingInfo(anyAuthor, anyTitle);
System.out.println(stateOfBook);
}
}
事实证明,另一个答案让我开始思考(我今天还没有这样做)。 显然我在我想要的东西的正确轨道上,我只需要重新安排东西。尽管下面的代码有效,但另一种方法更容易阅读。
public String checkForBookUsingInfo(){
int index = 0;
Book bookObject = null;
String returnValue = "Book not found";
String title = "";
String author = "";
Boolean isFound = false;
System.out.println("Please enter the name of a book to search for.");
String anyTitle = keybd.nextLine();
System.out.println("Please enter the name of an author to search for.");
String anyAuthor = keybd.nextLine();
while (index <bookList.size() && isFound == false ){
bookObject = bookList.get(index);
title = bookObject.getTitle();
author = bookObject.getAuthor();
if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
returnValue = "Book is in library.";
}
index++;
}
return returnValue;
}