I am working on a class called Bank that holds all the accounts in an arraylist, but first reads the accounts from a file and then creates each account. I am wondering what would be the best way to fix this problem so it will read properly and create each account, below are some files and other information.
first is the error message i get when i try to run the driver ( I already know essentially what this means i just do not know how to fix it and have the file read as well)
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextLong(Scanner.java:2196)
at java.util.Scanner.nextLong(Scanner.java:2156)
at Bank.load(Bank.java:81)
at BankDriver.main(BankDriver.java:15)
here is the file i am trying to read that contains the account information
Billy 1000 555-1111 100.00
Bob 1001 555-1112 101.00
Joe 1002 555-1113 102.00
Jonathan 1003 555-1114 103.00
Rick 1004 555-1115 104.00
here is the Account class
import java.text.NumberFormat; public class Account { private final double RATE = 0.03; // interest rate of 3.0% private long acctNumber; private double balance; private String name; private String phoneNumber; //----------------------------------------------- //Creates a template account //----------------------------------------------- public Account () { name = ""; acctNumber = 0000; phoneNumber = "555-1111"; balance = 0.0; } //--------------------------------------------------------------------------------------------- // Creates account by defining the owner, account number, phone number, and starting balance //--------------------------------------------------------------------------------------------- public Account (String owner, long account, String phone, double initial) { name = owner; acctNumber = account; phoneNumber = phone; balance = initial; } //----------------------------------------------------------------------- // Returns balance //----------------------------------------------------------------------- public double getBalance() { return balance; } //---------------------------------------------------------------------- // Returns account number //---------------------------------------------------------------------- public long getAcctNumber() { return acctNumber; } //-------------------------------------------------- // deposits money into the account //-------------------------------------------------- public double accDeposit (double amount) { balance = balance + amount; return balance; } //------------------------------------------------- //withdraws money from the account //------------------------------------------------- public double accWithdraw (double amount) { balance = balance - amount; return balance; } //------------------------------------------------ //adds interest to the account //------------------------------------------------ public double addInterest() { balance += (balance * RATE); return balance; } //------------------------------------------------- //Prints off the information for the account //------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return name + "\t" + acctNumber + "\t" + phoneNumber + "\t" + fmt.format(balance); } }
The Bank class
import java.util.Scanner; import java.util.ArrayList; import java.io.*; public class Bank { private ArrayList<Account> bank; private int count; //--------------------------------------------------------- //Constructs the Bank //--------------------------------------------------------- public Bank() { bank = new ArrayList<Account>(); } //---------------------------------------------------------- //Creates a new Account //---------------------------------------------------------- public void create (long accountNum, String owner, String phNumber, double initial) { bank.add (new Account(owner, accountNum, phNumber, initial)); count++; } //------------------------------------------------------------------------- //Desposits money into the account that matches the given account number //------------------------------------------------------------------------- public void deposit(long acctNum) { Scanner scan = new Scanner (System.in); System.out.println("How much would you like to deposit into account " +acctNum+ "?"); double depositAmount = scan.nextDouble(); findAcct(acctNum).accDeposit(depositAmount); } //----------------------------------------------------------------------- //withdraws money from the account that matches the given account number //----------------------------------------------------------------------- public void withdraw(long acctNum) { Scanner scan = new Scanner (System.in); System.out.println("How much would you like to withdraw from account " +acctNum+ "?"); double withdrawAmount = scan.nextDouble(); findAcct(acctNum).accWithdraw(withdrawAmount); } //---------------------------------------------- //Adds interest to all accounts //---------------------------------------------- public void Interest() { for (int i=0; i<count; i++) bank.get(i).addInterest(); } public void load(String file) throws IOException { Scanner fileScan, accountScan; String account, owner, phNumb; long acctNumb; double balance; fileScan = new Scanner (new File(file)); while (fileScan.hasNext()) { account = fileScan.nextLine(); accountScan = new Scanner(account); do { owner = accountScan.next(); acctNumb = accountScan.nextLong(); phNumb = accountScan.next(); balance = accountScan.nextDouble(); create(acctNumb, owner, phNumb, balance); } while (accountScan.hasNext()); } } public void save(String file)throws IOException { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println(toString()); out.close(); } //--------------------------------------------------- //Prints out all the accounts and their information //--------------------------------------------------- public String toString() { String report = ""; for (int i = 0; i<bank.size(); i++) report += bank.get(i) + "\n"; return report; } //---------------------------------------------------- //Finds the specified account and returns the account //---------------------------------------------------- private Account findAcct(long acctNum) { Account test = new Account(); for (int i=0; i<count; i++) { if (acctNum == bank.get(i).getAcctNumber()) test = bank.get(i); } return test; } }
and here is the bankdriver class
import java.io.*; public class BankDriver { public static void main (String[] args) throws IOException { Bank bank1 = new Bank();// creates bank bank1.load("BankAccountsDocument.rtf"); bank1.deposit(1000);//deposits money to account 1000 bank1.withdraw(1001);//withdraws money from account 1001 bank1.Interest();// adds interest to all accounts bank1.save("BankDocument.rtf");// prints all account information into the document System.out.println(bank1);//prints out all accounts } }