程序永远不会在ArrayList中找到一个项目,即使它应该在那里

时间:2015-02-16 12:45:43

标签: java arrays for-loop while-loop

我的任务是创建一个程序,从文本文件中读取数据,然后将数据存储在ArrayList中。然后用户可以输入一个整数,程序会检查该数字是否在数据库中。程序返回一个声明,说明该数字是否在数据库中。文本文件由各自的单独行组成,第一个数字表示文件中的整数。

当我运行我的程序时,即使我输入一个我知道在数据库中的数字,我仍然会得到“x不是数据库”。我做错了什么?

import java.io.*;
import java.util.*;
public class DirectoryLookupApplication {
   public static void main(String[] args) throws IOException {
   File read = new File("Data3.txt");
   Scanner dataFile = new Scanner(read);
   ArrayList<String> temporary = new ArrayList<String>();

   while (dataFile.hasNextLine()) {
      temporary.add(dataFile.nextLine());
   }
   temporary.remove(0); //removes the first element of the list since that element represents how many numbers are in the file
   Scanner userInput = new Scanner(System.in);
   System.out.println("Database Server is Ready for Number Lookups!");
   int searchingFor = userInput.nextInt();
   while (userInput.hasNext()) {
      searchingFor = userInput.nextInt();
      for (int i = 0; i < temporary.size(); i++) {
         if (temporary.contains(searchingFor)) {
            System.out.println("It exists in the database");
            break;
         }
         else {
            System.out.println("It is not in the database");
         }
     }
   }
   }
}

3 个答案:

答案 0 :(得分:2)

您的temporary列表包含String,但在temporary.contains(searchingFor),您正在检查它是否包含int。这永远不会真实。

也许你应该替换

int searchingFor = userInput.nextInt();

String searchingFor = userInput.nextLine();

答案 1 :(得分:0)

您的临时列表是一个字符串列表:

ArrayList<String> temporary = new ArrayList<String>();

来自System.in的用户输入读取为整数:

int searchingFor = userInput.nextInt();

您正在尝试检查字符串列表是否包含整数。 Java不是perl,它不转换类型。 修改代码,使temporary列表包含Integer个对象,或者从String读取userInput

答案 2 :(得分:0)

当您比较两种不同的变量类型时会发生这种情况。您的ArrayList具有String值,您正在比较searchFor,它是一个整数值。这可以通过使用字符串值或整数值来解决。

我在你的代码中发现了另外两个问题。

  1. 不会使用数据库检查从扫描仪获得的第一个输入。您必须输入要检查的第二个值。
  2. 您还必须在else语句中添加break。否则循环将继续。 我在以下代码中解决了这些问题。

    public static void main(String[] args) throws IOException {
    
    File read = new File("Data3.txt");
    Scanner dataFile = new Scanner(read);
    ArrayList<String> temporary = new ArrayList<String>();
    
    while (dataFile.hasNextLine()) {
        temporary.add(dataFile.nextLine());
    }
    
    temporary.remove(0); //removes the first element of the list since that element represents how many numbers are in the file
    
    Scanner userInput = new Scanner(System.in);
    System.out.println("Database Server is Ready for Number Lookups!");
    
    String searchingFor = ""; 
    
    while (userInput.hasNext()) {
        searchingFor = userInput.nextLine();  // gets input 
        for (int i = 0; i < temporary.size(); i++) {
            if (temporary.contains(searchingFor)) {
                System.out.println("It exists in the database");
                break;
            } else {
                System.out.println("It is not in the database");
                break;  // added break point 
            }
        }
    
    }}