Question: Java error messages java.lang.ArrayIndexOutOfBoundsException: 1 Can someone please tell me what's wrong and how to fix this? enum AccountEnum{Savings,Checking,Business,CreditCard} public class Account { // data
Java error messages
java.lang.ArrayIndexOutOfBoundsException: 1
Can someone please tell me what's wrong and how to fix this?
enum AccountEnum{Savings,Checking,Business,CreditCard} public class Account {
// data fields
private int ACCNumber;
private double ACCBalance;
private String CustomerID;
private String DebitNo;
private String BName;
private String BID;
private String CreditNo;
private double MaxCredit; private AccountEnum Type;
//Savings Account
public Account(int ACCNumber, String CustomerID, double ACCBalance) {
this.ACCNumber = ACCNumber;
this.CustomerID = CustomerID;
this.ACCBalance = ACCBalance; Type = AccountEnum.Savings;} //Checking Account
public Account(int ACCNumber, String CustomerID, double ACCBalance, String DebitNo)
{
this.ACCNumber = ACCNumber;
this.CustomerID = CustomerID;
this.ACCBalance = ACCBalance;
this.DebitNo = DebitNo;
Type = AccountEnum.Checking; }
//Business Account
public Account (int ACCNumber, String CustomerID, double ACCBalance, String BName, String BID)
{
this.ACCNumber = ACCNumber;
this.CustomerID = CustomerID;
this.ACCBalance = ACCBalance;
this.BName = BName;
this.BID = BID;
Type = AccountEnum.Business; }
//Credit Card Account
public Account(String CustomerID, String CreditNo, double ACCBalance, double MaxCredit)
{
this.CustomerID = CustomerID;
this.CreditNo = CreditNo;
this.ACCBalance = ACCBalance;
this.MaxCredit = MaxCredit;
Type = AccountEnum.CreditCard; }
public String getCreditNo() {return CreditNo;}
public void setCreditNo(String CreditNo) {this.CreditNo = CreditNo;}
public double getMaxCredit() {return MaxCredit;}
public void setMaxCredit(double MaxCredit) {this.MaxCredit = MaxCredit;}
public String getDebitNo() {return DebitNo;}
public void setDebitNo(String DebitNo) {this.DebitNo = DebitNo;}
public String getBID() {return BID;}
public void setBID(String BID) {this.BID = BID;}
public String getBName() {return BName;}
public void setBName(String BName) {this.BName = BName;}
public String getCustomerID() {return CustomerID;}
public void setCustomerID(String CustomerID) {this.CustomerID = CustomerID;}
public int getACCNumber() {return ACCNumber;}
public void setACCNumber(int ACCNumber) {this.ACCNumber = ACCNumber;}
public double getACCBalance() {return ACCBalance;}
public void setACCBalance(double ACCBalance) {this.ACCBalance = ACCBalance;} @Override public String toString() {
return "Account [ACCNumber=" + ACCNumber + ", ACCBalance=" + ACCBalance + ", CustomerID=" + CustomerID
+ ", DebitNo=" + DebitNo + ", BName=" + BName + ", BID=" + BID + ", CreditNo=" + CreditNo
+ ", MaxCredit=" + MaxCredit + "]";
}
}
public class Customer{
private String CustomerID;
private String LName;
private String FName;
private String Address;
private String City;
public Customer(String CustomerID, String LName, String FName, String Address, String City)
{
this.CustomerID =CustomerID;
this.LName = LName;
this.FName = FName;
this.Address = Address;
this.City = City;
}
public String getCustomerID() {return CustomerID;}
public void setCustomerID(String CustomerID) {this.CustomerID = CustomerID;}
public String getLName() { return LName;}
public void setLName(String LName) {this.LName = LName;}
public String getFName() {return FName;}
public void setFName(String FName) {this.FName = FName;}
public String getAddress() {return Address;}
public void setAddress(String Address) {this.Address = Address;}
public String getCity() {return City;}
public void setCity(String City) {this.City = City;}
@Override
public String toString() {
return "CustomerID+LName +FName + Address +City";
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
ArrayList customers;
ArrayList accounts;
public Main() {
customers = new ArrayList();
accounts = new ArrayList();
}
public void getDataFromFile(String dataset){
Scanner sc;
try{
//file
File file = new File(dataset);
//scanner
sc = new Scanner(file);
//read line
while ( sc.hasNextLine()) {
//split line
String[] tokens = sc.nextLine().split("\\s{2}");
Customer customer = new Customer(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);
customers.add(customer);
if (tokens[5].equalsIgnoreCase("Checking")){
Account account= new Account(Integer.parseInt(tokens[6]), tokens[0], Double.parseDouble(tokens[7]), tokens[8]);
accounts.add(account);
}
if (tokens[5].equalsIgnoreCase("Savings")){
Account account= new Account(Integer.parseInt(tokens[6]), tokens[0],Double.parseDouble( tokens[7]));
accounts.add(account);
}
if (tokens[5].equalsIgnoreCase("Business")){
Account account= new Account(Integer.parseInt(tokens[6]), tokens[0], Double.parseDouble(tokens[7]), tokens[8], tokens[9]);
accounts.add(account);
}
if (tokens[5].equalsIgnoreCase("CreditCard")){
Account account= new Account(tokens[0], tokens[6], Double.parseDouble(tokens[7]), Double.parseDouble(tokens[8]));
accounts.add(account);
}
}
}
catch(Exception e){
System.err.println(e);
}
}
//prints both arrays using for loop
public void displayAccounts(){
for (Customer customer:customers){
System.out.println(customer.toString());
for (Account account:accounts){
if (account.getCustomerID() == customer.getCustomerID())
System.out.println(account.toString());
}
}
}
public static void main(String[] args) {
Main main = new Main();
//you can change file name here
main.getDataFromFile("Dataset.txt");
main.displayAccounts();
}
}
Dataset.txt (
C8392380567 Sage Amy PingTingRoad Edmonton Checking 873387 5000 0000-6666-6666-6666
C8954385123 Lee Bob TexacoRoad Calgary Savings 827366 9480
C2389490434 Neil Carson DeerfootTrail Otawa Business 763655 65000 EmporiaLLC 87-927736
C9384899234 Ko David UnversityDrive Stillwater CreditCard 7667-9899-8776-1234 430 4000
C0930238083 Warren John OgdenRoad Tyler Checking 726615 1230 0000-2222-2222-2222
C8392380567 C8954385123 C2389490434Ne Carson Deerfoot Trai Otawa C9384899234 CO930238083 Checking Savings Bus iness CreditCard Checking 873387 5000 0000-6666-6666-6666 827366 9480 763655 65000 EmporiaLLC 7667-9899-8776-1234 4304000 726615 230 0000-2222-2222-2222 Sage Amy Bob Pingl ingHoad TexacoRoad EdmontoN Calgary 87-927736 David UnversityDrive Stillwater Warren JohnOgdenRoad Tyler
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
