*How can i make sure my validAccounts array does not have null values?**
This program is an ATM program that uses a text file and checks to see if the user's password and account number matches the possible passwords and balance from the text file
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;
}
}