运行我的代码时遇到问题。它编译得很好,但是当我运行它时,我得到了这个错误。data3.txt 文件是一串数字。
错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at database3.main(database3.java:6)
我的代码:
import java.io.*;
import java.util.*;
public class database3 {
public static void main(String[] args) {
File inputDataFile = new File(args[0]);
Scanner inputFile = new Scanner("data3.txt"); // READ DATA FROM FILE
int foundvalue = 0;
int d = inputFile.nextInt();
int list[] = new int[d];
for (int i = 0; i < d; i++)
list[i] = inputFile.nextInt();
System.out.println("Database Server is Ready for Number Lookups!");
Scanner stdin = new Scanner(System.in); //Get user input
double input;
while (stdin.hasNext()){
input = stdin.nextDouble();
boolean found = false;
System.out.println("The number to look up is: " +input);
for (int j = 0; j < d; j++){
if(list[j] == input){
found = true;
break;
}
}
if(found == true){
System.out.println(input +" is in the database");
}
else{
System.out.println(input +" is NOT in the database");
}
}
System.out.println("Goodbye!");
System.exit(0);
}
}