2014-03-05 2 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

네 동생 이잖아 컴파일

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

getBalance 방법은 ArrayList를 Balance하지만 빈 그렇게 던져 예외에서 요소를 얻을 : 클래스 계정방법은 균형을 아래와 같이 방법을 얻을 수 있습니다 문제 : ( Arraylist에서 무언가를 인쇄하려고 시도 할 때마다 스레드 메인 오류 에서 실행을 계속 유지합니다. – user3384923

+3

예외 상태로 비어있을 때 'Balance' 인덱스 0에 액세스하려고 시도합니다. 넌 못해. – BobTheBuilder

답변

3

당신은 어떤 요소를 추가하지 않은 도움말 ArrayList에 Balance 아래 라인에서 고객에 대한 균형을 얻으려고 노력 :

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

전화내가 코드를 생각 DNT

+0

여전히 .. ..이 문장을 제거하려고했는데 ... succesfuly 을 입금하려고 시도했지만이 문제가 다시 발생했을 때 .... – user3384923

+0

예외 추적을 통과 할 수 있습니다. @ user3384923 – Kick

+0

tempBal = show.getBalance (num); @ user3384923 입금 방법 – Kick