Reputation: 5
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
Upvotes: 0
Views: 2229
Reputation: 495
A few problems with your code are:
Database
class (which should be named Account
). string
s to the actual data types (int
and double
). catch(Exception)
.A possible solution to your code could be this (I have not tested if it actually works):
private static String getLineContent(String value) {
return value.substring(value.indexOf(' ') + 1);
}
public static void main(String[] args) {
BufferedReader in;
try {
in = new BufferedReader(new FileReader("filefullofmoney.txt"));
} catch (FileNotFoundException ex) {
// TODO: Handle the error with a nice error message.
return;
}
List<Account> accounts = new ArrayList<Account>();
while (true) {
try {
String accountName = in.readLine();
if (accountName == null) {
// We have no new accounts. So we exit.
break;
}
accountName = getLineContent(accountName);
int pin = Integer.parseInt(getLineContent(in.readLine()));
double balance = Double.parseDouble(getLineContent(in.readLine()));
accounts.add(new Account(accountName, pin, balance));
} catch (IOException ex) {
// TODO: Handle the error with a nice message saying that the file is malformed.
}
}
}
class Account {
public String accountName;
public int pin;
public double balance;
public Account(String accountName, int pin, double balance) {
this.accountName = accountName;
this.pin = pin;
this.balance = balance;
}
}
Upvotes: 2
Reputation: 600
You should probably change
Database[] accounts = new Database[numberOfLines];
to be
ArrayList<String[]> accounts = new ArrayList<String[]>();
then in the loop where you have
accounts.add(new Database(array[0],array[1],array[2]));
replace it with
accounts.add(array1);
Also, You never incremented J so your program will only get the first record.
If you have to use the Database class you have created, then you need to add a constructor that accepts those parameters.
Upvotes: 0
Reputation: 5952
Here:
BufferedReader in = new BufferedReader(new FileReader("filefullofmoney.txt"));
String line;
StringBuilder account = new StringBuilder();
List<Database> accounts = new ArrayList<Database>();
int count = 1;
while ((line = in.readLine()) != null) {
account.append(line);
account.append("---")
if (count % 3 == 0) {
Database data = new Database();
data.accountName = account.toString().split("---")[0];
data.pin = Integer.parseInt(account.toString().split("---")[1]);
data.balance = Double.parseDouble(account.toString().split("---")[2]);
accounts.add(data);
account = new StringBuilder();
}
count++;
}
in.close();
Explanation:
The file is being read and every 3 lines (name, pin, balance), the data is added the the accounts List (% means modulus which returns the remainder when you divide). Since the pin and balance are not Strings, you have to parse the String to an integer and a double respectively.
Upvotes: 0
Reputation: 68887
Rename the class to Account
instead of Database
.
Then: the readLine() method will return the whole line, so:
Account1 Martijn Courteaux
will be one line. To take the name from it, you can use String.substring()
:
String line = null; // Temp
String name = (line = in.readLine()).substring(line.indexOf(' '));
Do the same for the two other values:
int pin = Integer.parseInt((line = in.readLine()).substring(line.indexOf(' ')));
double balance = Double.parseDouble((line = in.readLine()).substring(line.indexOf(' ')));
Then use the values to pass them to the constructor:
accounts.add(new Account(name, pin, balance));
Upvotes: 0
Reputation: 30032
You are using array1
and array
choose one and it will compile.
Also you should combine those two while loops. Hint: you don't need to know the numberOfLines
.
Upvotes: 1