I imported a List of Strings from a file and put them into an arrayList. I am trying to chop of the end of these arrays, so I’m putting them into a separate String format
Here is where i set x
x = new ArrayList<BankAccounts>();
try {
Scanner reader;
reader = new Scanner( new File("F:\\myCreditUnion.txt") );
while ( reader.hasNext() )
{
String inputLine = reader.nextLine();
first = inputLine.substring(0, 3);
second = Double.parseDouble(inputLine.substring(5, inputLine.length()));
x.add(new BankAccounts(first, second));
}
reader.close();
}
and this is where i try to chop off the end
double howmuch;
for(int i = 0; i < x.size(); i++)
{
list.equals(x.get(i));
howmuch = Double.parseDouble(list.substring(5, list.length()));
}
// x is the list
I am getting a nullpointerexception. Wondering how to fix this, as I am pretty new to programming.
Document i am importing contains a combinations of # and letters such as, 101s 583.58
To assign a value to
listyou have uselist = x.get(i);instead oflist.equals(x.get(i));Update:
equals()does not assign values it only checks if two objects are equal.The value returned by x.get(i) will be a BankAccount so you can’t assign it to list (which is a String)
You have to either turn the BankAccount into a String by using toString() or you have to get a String out of it by calling one of the BankAccount methods before assigning it to list, how this works depends on the methods provided by the BankAccount class.