For my homework assignment, I’m supposed to create an ATM/Teller program which stores users accounts in a text file. I require help reading the text file and storing certain parts of it in an array list.
import java.io.*;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.BufferedReader;
public class GetData
{
public static void main(String[] args)
{
BufferedReader in = new BufferedReader(new FileReader("filefullofmoney.txt"));
String strLine;
int numberOfLines = 0;
while ((strLine = in.readLine()) != null)
{
numberOfLines++;
}
Database[] accounts = new Database[numberOfLines];
String[] array1 = new String[3];
int i;
int j = 0;
while (j < numberOfLines)
{
for (i=0; i < 2; i++)
{
array1[i] = in.readLine();
}
accounts.add(new Database(array[0],array[1],array[2]));
}
}
}
class Database
{
public String accountName;
public int pin;
public double balance;
}
The part I’m having trouble with is accounts.add(new Database(array[0],array[1],array[2]));
Basically my text file will be formatted in this way:
Account1 name
Account1 pin
Account1 balance
Account2 name
Account2 pin
Account2 balance
etc...
I want to be able to add the 3 lines of text for each account into one element on the arraylist.
I’m not sure how much of my could actually works because I can’t get it to compile.
Any help is greatly appreciated. Thanks
A few problems with your code are:
Databaseclass (which should be namedAccount).strings to the actual data types (intanddouble).catch(Exception).A possible solution to your code could be this (I have not tested if it actually works):