【问题标题】:Program never finds an item in an ArrayList, even though it is supposed to be there程序永远不会在 ArrayList 中找到项目,即使它应该在那里
【发布时间】:2015-04-16 23:33:36
【问题描述】:

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

当我运行我的程序时,即使我在数据库中输入了一个我知道是 IS 的数字,我也会不断收到“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");
         }
     }
   }
   }
}

【问题讨论】:

  • 尝试打印出您的变量,以检查它们是否符合您的想法。
  • 你能发布一个文件内容的例子吗?它可以提供帮助。
  • 我不是 Java 专家,但您的意思是 temporary[i].contains(searchingFor)
  • 不要破坏自己的问题。如果您删除代码,那么问题和答案对其他人来说就是零意义。

标签: java arrays for-loop while-loop


【解决方案1】:

您的temporary 列表包含Strings,但在temporary.contains(searchingFor) 中,您正在检查它是否包含int。这永远不会返回 true。

也许你应该替换

int searchingFor = userInput.nextInt();

String searchingFor = userInput.nextLine();

【讨论】:

    【解决方案2】:

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

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

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

    int searchingFor = userInput.nextInt();
    

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

    【讨论】:

      【解决方案3】:

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

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

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

        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 
                }
            }
        
        }}
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        • 2014-04-09
        相关资源
        最近更新 更多