Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at Account.getBalance(Account.java:51)
at Account.main(Account.java:95)
这是完整的代码
import java.util.Scanner;
import java.util.ArrayList;
public class Account
{
ArrayList<String> Name = new ArrayList<String>();
ArrayList<String> Cnic = new ArrayList<String>();
ArrayList<Integer> Age = new ArrayList<Integer>();
ArrayList<Double> Balance = new ArrayList<Double>();
double amount;
/*Setting values */
public void setName(String name1)//Name
{
Name.add(name1);
}
public void setCnic(String cnic1)//Cnic
{
Cnic.add(cnic1);
}
public void setAge(int age1)//Age
{
Age.add(age1);
}
public void setBalance(double bal)//Balance
{
Balance.add(bal);
}
public void setSpecificBalance(int num, double bal)
{
Balance.add(num,bal);
}
/*End of set methods */
/*Getting Values */
public String getName(int num)//Name
{
return Name.get(num);
}
public String getCnic(int num)//Cnic
{
return Cnic.get(num);
}
public int getAge(int num)//Age
{
return Age.get(num);
}
public double getBalance(int num)//Balance
{
return Balance.get(num);
}
public void printAccountsTill(int num)//Printing all accounts till a specified number
{
for(int x=0; x<num; x++)
{
System.out.printf("\nName of Customer is: %s\n",Name.get(x));
System.out.printf("\nCnic of Customer is: %S\n",Cnic.get(x));
System.out.printf("\nAge of Custmoer is: %d\n",Age.get(x));
System.out.printf("\nBalance is : %lf\n\n",Balance.get(x));
}
}
/*End of get methods */
/*Start of Main Method */
public static void main(String[] arg)
{
Scanner inputI = new Scanner(System.in);
Scanner inputL = new Scanner(System.in);
Account cust = new Account();
String name,cnic;
int age,temp=1,temp1=0,temp2,check;
int numb, dep, with;
while(temp!=0)
{
if(temp1==0)
{
System.out.print("\nEnter name of customer: ");
name = inputL.nextLine();
cust.setName(name);
System.out.print("\nEnter cnic of customer: ");
cnic = inputL.nextLine();
cust.setCnic(cnic);
System.out.print("\nEnter age of customer: ");
age = inputI.nextInt();
cust.setAge(age);
Balance(age);
System.out.printf("\nAccount no. is: %d\n",temp1);
System.out.printf("\nBalance of customer is: %lf\n\n",cust.getBalance(temp1));
}
else
{
System.out.printf("\nTo create a new account enter 1 or enter anything else for Transaction: ");
temp2 = inputI.nextInt();
if(temp2==1)
{
System.out.print("\nEnter name of customer: ");
name = inputL.nextLine();
cust.setName(name);
System.out.print("\nEnter cnic of customer: ");
cnic = inputL.nextLine();
cust.setCnic(cnic);
System.out.print("\nEnter age of customer: ");
age = inputI.nextInt();
cust.setAge(age);
Balance(age);
System.out.printf("\nAccount no. is: %d\n",temp1);
System.out.printf("\nBalance of customer is: %lf\n\n",cust.getBalance(temp1));
}
else
{
System.out.print("\n\nFor Deposit: 1\nFor Widthraw: 2: \n Enter Anything else to exit: ");
check = inputI.nextInt();
if(check==1)
{
System.out.print("\n\nEnter the number of account: ");
numb = inputI.nextInt();
System.out.print("\nEnter the amount: ");
dep = inputI.nextInt();
Deposit(numb,dep);
}
else if(check==2)
{
System.out.print("\n\nEnter the number of account: ");
numb = inputI.nextInt();
System.out.print("\nEnter the amount: ");
with = inputI.nextInt();
Withdraw(numb,with);
}
}
}
System.out.print("\nEnter 0 to terminate program or enter anything else to continue: ");
temp = inputI.nextInt();
temp1++;
}/*End of While Loop */
}/*End of Main Method */
public static void Balance(int age)
{
Account give = new Account();
if(age>35)
{
give.setBalance(0);
}
else if(age>30 && age<35)
{
give.setBalance(10000);
}
else if(age>25 && age<=30)
{
give.setBalance(25000);
}
else if(age>=20 && age<=25)
{
give.setBalance(50000);
}
else
System.out.print("\nAge limit is greater than 20\n");
}
public static void Deposit(int num, double amount)
{
Account show = new Account();
double tempBal = 0;
double bonus;
tempBal = show.getBalance(num);
System.out.printf("\n\nCurrent balance is: %lf\n",tempBal);
bonus = (amount/100)*5;
amount = amount + bonus;
tempBal = tempBal + amount;
System.out.printf("New balance is: %lf\n\n",tempBal);
show.setSpecificBalance(num,tempBal);
}
public static void Withdraw(int num, double amount)
{
Account show = new Account();
double tempBal = 0;
double tax;
tempBal = show.getBalance(num);
System.out.printf("\n\nCurrent balance is: %lf\n",tempBal);
tax = (amount/100)*10;
amount = amount + tax;
tempBal = tempBal - amount;
System.out.printf("New balance is: %lf\n\n",tempBal);
show.setSpecificBalance(num,tempBal);
}
}
请!帮助一点:(
答案 0 :(得分:3)
您尚未在arrayList Balance
上添加任何元素,并尝试在下面的行中获取客户的余额:
System.out.printf("\nBalance of customer is: %lf\n\n",cust.getBalance(temp1));
调用类帐户的getBalance
方法获取余额,方法如下:
public double getBalance(int num)//Balance
{
return Balance.get(num);
}
getBalance
方法从arrayList Balance
获取元素,但是它为空,因此抛出异常