2014-03-05 35 views
-4
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

我硝基甲苯認爲代碼編譯成功 – Kick

+0

是哥哥,多數民衆贊成問題:( 它不斷給線程主錯誤 執行任務,每當我嘗試打印從Arraylist東西它給出了這個錯誤 – user3384923

+3

作爲例外狀態,你嘗試訪問'平衡'索引0,當它是空的。你不能做到。 – BobTheBuilder

回答

3

您還沒有添加任何元素ArrayList中Balance並試圖獲得對客戶在下面一行的平衡:

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

電話類賬戶方法來取得平衡,方法如下:

public double getBalance(int num)//Balance 
{ 
    return Balance.get(num); 
} 

getBalance方法從ArrayList中Balance但其空的,所以拋出異常獲得元素

+0

仍然,..我試圖刪除這個聲明,..它編譯succesfuly ,但當我試圖作出定金這件事情再次發生,...... – user3384923

+0

可以通過例外跟蹤,似乎更多的錯誤存在@ user3384923 – Kick

+0

相同與tempBal = show.getBalance(num); @ user3384923在存款方式 – Kick