*如何确保我的validAccounts数组没有空值?**
该程序是一个ATM程序,它使用文本文件并检查用户的密码和帐号是否与文本文件中可能的密码和余额相匹配
import java.util.*;
import java.io.*;
public class ATM2 {
public static Scanner kbd;
public static final int MAXSIZE = 60000;
public static void main(String[] args) {
kbd = new Scanner(System.in);
Scanner input = null;
String[] validAccounts = new String[MAXSIZE];
int lineNum = 1;
try {
input = new Scanner(new File("ATMdata.txt"));
// counts how many lines are in text
while (input.hasNextLine()) {
String line;
line = input.nextLine();
lineNum++;
}
// sets the array to the number of lines in text
validAccounts = new String[lineNum];
// prints out the number of lines in text
System.out.println(lineNum);
input.close();
}
catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
try {
int count = 0;
input = new Scanner(new File("ATMdata.txt"));
// counts how many lines are in text
while (input.hasNextLine()) {
String line;
line = input.nextLine();
validAccounts[count] = line;
count++;
System.out.println(line);
}
input.close();
} catch (FileNotFoundException e) {
System.out.println("There was an error opening file");
}
System.out.println("What is your account number?");
String acctNum = kbd.nextLine();
System.out.println("What is the password?");
String pwd = kbd.nextLine();
checkID(acctNum, pwd,validAccounts);
}
// Each entry in the validAccounts array is assumed to be holding:
// the account number followed by
// a space, followed by
// the password for the account
// followed by a space
// followed by the current balance.
public static String checkID(String acctNum, String pwd,
String[] validAccounts) {
String account = "" ;
String password = "" ;
String balance;
String result = "error";
//This is supposed to check the array for a valid input
for(int i = 0; i <validAccounts.length ; i ++){
**//It is giving me an error for the line below-if anyone could help me figure it out!**
int space1 = validAccounts[i].indexOf(" ");//ERROR
account = validAccounts[i].substring(0, space1);
int space2 = validAccounts[i].indexOf(" ", space1 + 1);
password = validAccounts[i].substring(validAccounts[i].indexOf(" ") + 1, space2);
balance = validAccounts[i].substring(validAccounts[i].lastIndexOf(" ") + 1);
}
if (acctNum.equals(account)){
return account;
}
if(pwd.equals(password)){
return password;
} return result;
}
}
答案 0 :(得分:1)
TL; DR :在input = new Scanner(new File("ATMdata.txt"));
上设置断点并评估input.hasNextLine()
- 这将是错误的!可能是路径问题?
明显的失败模式是没有元素添加到validAccounts
数组中。
IIRC Scanner.nextLine()
不会返回null
因此问题必须是while (input.hasNextLine()) {
块永远不会输入,并且因为你有
String[] validAccounts = new String[MAXSIZE];
(这将是60k null
)你有一个NPE。您应该阅读java.util.List
干杯,
答案 1 :(得分:0)
public static String checkID(String acctNum, String pwd,
String[] validAccounts) {
String account = "" ;
String password = "" ;
String balance;
String result = "error";
//This is supposed to check the array for a valid input
for(int i = 0; i <validAccounts.length ; i ++){
if(validAccounts[i]!=null){
int space1 = validAccounts[i].indexOf(" ");//ERROR
account = validAccounts[i].substring(0, space1);
int space2 = validAccounts[i].indexOf(" ", space1 + 1);
password = validAccounts[i].substring(validAccounts[i].indexOf(" ") + 1, space2);
balance = validAccounts[i].substring(validAccounts[i].lastIndexOf(" ") + 1);
}
}
if (acctNum.equals(account)){
return account;
}
if(pwd.equals(password)){
return password;
}
return result;
}