Java 不知道如何在主线程中执行这些异常:(

Java 不知道如何在主线程中执行这些异常:(,java,Java,这是完整的代码 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:

这是完整的代码

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;
导入java.util.ArrayList;
公共类帐户
{
ArrayList Name=新的ArrayList();
ArrayList Cnic=新的ArrayList();
ArrayList年龄=新建ArrayList();
ArrayList余额=新的ArrayList();
双倍金额;
/*设定值*/
public void setName(字符串名称1)//名称
{
名称。添加(名称1);
}
public void setCnic(字符串cnic1)//Cnic
{
Cnic.add(cnic1);
}
公共无效设置(int age1)//Age
{
年龄。添加(年龄1);
}
公共无效余额(双倍余额)//余额
{
余额。添加(bal);
}
公共无效设置规范平衡(整数,双平衡)
{
余额。添加(num,bal);
}
/*集合结束方法*/
/*获取价值*/
公共字符串getName(int num)//Name
{
返回Name.get(num);
}
公共字符串getCnic(int num)//Cnic
{
返回Cnic.get(num);
}
public int getAge(int num)//年龄
{
返回Age.get(num);
}
public double getBalance(int num)//Balance
{
返回余额。获取(num);
}
public void printAccountsTill(int num)//打印所有帐户直到指定的数字
{
对于(int x=0;x35)
{
给.立根平衡(0);
}

否则,如果(年龄>30&&age25&&age=20&&age您未在arrayList
余额中添加任何元素,并尝试在下面的行中为客户获取余额:

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);
}
    }
调用类科目的
getBalance
方法获取余额,方法如下:

   System.out.printf("\nBalance of customer is: %lf\n\n",cust.getBalance(temp1));

getBalance
方法从arrayList获取元素
Balance
但它为空,因此抛出异常

我不认为代码编译成功是的兄弟,这就是问题所在:(每当我试图从Arraylist打印某些内容时,它总是在线程主错误中给出Execution它给出此错误作为异常状态,当索引0为空时,您尝试访问它。您不能这样做。
getBalance
正在尝试访问不存在的索引。您应该使用调试器逐步检查y。)我们的程序,看看到底传递了什么,以及为什么。任何解决方案,…我是一个新手..不知道太多..尽管如此,…我尝试删除此语句,…它编译成功,但当我尝试存款时,这件事再次发生,…你能传递异常跟踪吗,似乎存在更多错误@user3384923same with tempBal=show.getBalance(num);@user3384923存款method@user3384923首先在列表中添加客户详细信息,然后存款..无论您想做什么,亲爱的lc@user3384923
public double getBalance(int num)//Balance
{
    return Balance.get(num);
}