Java 程序从未在ArrayList中找到项,即使它应该在ArrayList中

Java 程序从未在ArrayList中找到项,即使它应该在ArrayList中,java,arrays,for-loop,while-loop,Java,Arrays,For Loop,While Loop,我的任务是创建一个程序,从文本文件中读取数据,然后将数据存储在ArrayList中。然后用户可以输入一个整数,程序检查该数字是否在数据库中。程序返回一条语句,说明该数字是否在数据库中。文本文件由每个单独行上的整数组成,第一个数字表示文件中的整数数 当我运行我的程序时,即使我输入了一个我知道在数据库中的数字,我仍然会得到“x不是数据库”。我做错了什么 import java.io.*; import java.util.*; public class DirectoryLookupApplicat

我的任务是创建一个程序,从文本文件中读取数据,然后将数据存储在
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");
         }
     }
   }
   }
}
import java.io.*;
导入java.util.*;
公共类DirectoryLookupApplication{
公共静态void main(字符串[]args)引发IOException{
文件读取=新文件(“Data3.txt”);
扫描仪数据文件=新扫描仪(已读);
ArrayList temporary=新的ArrayList();
while(dataFile.hasNextLine()){
temporary.add(dataFile.nextLine());
}
temporary.remove(0);//删除列表的第一个元素,因为该元素表示文件中有多少个数字
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“数据库服务器已准备好进行数字查找!”);
int searchingFor=userInput.nextInt();
while(userInput.hasNext()){
searchingFor=userInput.nextInt();
对于(int i=0;i
您的
临时
列表包含
字符串
s,但在
临时.包含(搜索)
中,您正在检查它是否包含
int
。这永远不会变成现实

也许你应该换一个

int searchingFor = userInput.nextInt();


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

ArrayList<String> temporary = new ArrayList<String>();
您正在尝试检查字符串列表是否包含整数。Java不是perl,它不转换类型。
修改代码,使
临时
列表包含
整数
对象,或从
用户输入
读取
字符串
,这是在比较两种不同的变量类型时发生的。您的ArrayList具有字符串值,您正在比较searchingFor,它是一个整数值。这可以通过使用字符串值或整数值来解决

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

  • 从扫描仪获得的第一个输入不会与数据库进行检查。必须输入第二个值进行检查
  • 您还必须在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 
            }
        }
    
    }}
    
    publicstaticvoidmain(字符串[]args)引发IOException{
    文件读取=新文件(“Data3.txt”);
    扫描仪数据文件=新扫描仪(已读);
    ArrayList temporary=新的ArrayList();
    while(dataFile.hasNextLine()){
    temporary.add(dataFile.nextLine());
    }
    temporary.remove(0);//删除列表的第一个元素,因为该元素表示文件中有多少个数字
    扫描仪用户输入=新扫描仪(System.in);
    System.out.println(“数据库服务器已准备好进行数字查找!”);
    字符串搜索“”;
    while(userInput.hasNext()){
    searchingFor=userInput.nextLine();//获取输入
    对于(int i=0;i

  • 试着打印出你的变量,检查它们是否与你认为的一样。你能举一个文件内容的例子吗?这可能会有帮助。我不是java专家,但你是说
    临时[I].contains(searchingFor)
    ?@Sonam Avichal-不要破坏你自己的问题。如果你删除代码,那么问题和答案对其他人来说毫无意义。
    int searchingFor = userInput.nextInt();
    
    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 
            }
        }
    
    }}