-4

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

}

user3546001
  • 21
  • 1
  • 2
  • 6

2 Answers2

1

TL;DR: Set a breakpoint on the input = new Scanner(new File("ATMdata.txt")); and evaluate input.hasNextLine() - it'll be false! Possibly a path problem?

The obvious failure mode would be that no elements are being added to your validAccounts array.

IIRC Scanner.nextLine() doesn't return null so the problem has to be that the while (input.hasNextLine()) { block is never entered, and since you have

String[] validAccounts = new String[MAXSIZE];

(which will be 60k null's) you got an NPE. You should read up on java.util.List

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
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;
}
Patrick Chan
  • 1,019
  • 10
  • 14
  • I tried this and now it is not printing anything at all – user3546001 Dec 08 '14 at 14:29
  • yes, this code protected your program from crashing, but it proves that validAccounts[i] is null inside the loop. So the problem now is why there's a null value in this array – Patrick Chan Dec 08 '14 at 14:31
  • I added a print statement that checks to see if the array has the contents of the text file and it seems to be printing out correctly – user3546001 Dec 08 '14 at 14:34