2012-03-28 138 views
0

所以即時通訊基本上試圖做一個「銀行」計劃。想知道如何從.dat文件中讀取對象

到目前爲止(我認爲)我已經獲得了將對象寫入.dat文件供將來使用的方法,並且我希望在每次啓動時將這些對象讀入ArrayList,以便您可以訪問以前創建的帳戶。

我不斷收到 「java.lang.IndexOutOfBoundsException」 在mainBank.main(mainBank.java:22)

這裏是mainBank代碼:

import java.util.*; 
    import java.io.*; 

    public class mainBank 
    { 
private static ArrayList<Account> userList = new ArrayList<Account>(); 
private static int userNum; 
private static Scanner scan = new Scanner(System.in); 
public static void main(String args[]) throws IOException 
{ 
    try 
    { 
     readFromArray(); 
    } 
    catch(Exception e) 
    { 
     e.getStackTrace(); 
    } 
    System.out.println("Welcome to the bank"); 
    System.out.println("Account ID"); 
     int uID = scan.nextInt(); 
    Account currAccount = new Account((Account)userList.get(uID)); 

    nextAction(currAccount); 

} 

public static void nextAction(Account acc) 
{ System.out.println(acc); 

    System.out.print("Are you happy with this y/n? "); 
    String input = scan.nextLine(); 
if(input.toLowerCase().equals("y")) 
{ 
    System.out.println("Thank you Come Again"); 
} 
else 
{ 
    System.out.println("What would you like to do?"); 
    System.out.println("1.Deposit"); 
    System.out.println("2.Widthdraw"); 
    System.out.println("3.Exit"); 
     int choice = scan.nextInt(); 
    switch(choice) 
    { 
    case 1: System.out.print("Deposit Ammount: "); 
      acc.deposit(scan.nextInt()); 
      nextAction(acc); 

      break; 

    case 2: System.out.print("Widthdrawl Ammount: "); 
      try{acc.withdraw(scan.nextInt());} 
      catch(Exception e){System.out.println("not enough money");} 
      nextAction(acc); 

      break; 

    case 3: System.out.print("Okay, Good Bye!"); 
      break; 
    } 
} 


} 
public static void readFromArray() throws IOException, Exception 
{ 
    FileInputStream fis = new FileInputStream("data.dat"); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    Account acc = new Account((Account) ois.readObject()); 


    for(int i = 0; i<userNum;i++) 
    {  
     acc = (Account) ois.readObject(); 
     userList.add(acc); 
    } 
} 
public static void writeToFile() throws IOException 
{ 
    FileOutputStream fos = new FileOutputStream("data.dat"); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 


    for(int i =0;i<userList.size();i++) 
    { 
     Account acc = userList.get(i); 
     oos.writeObject(acc); 
    } 
    oos.flush(); 
    oos.close(); 
} 
    } 

Account類:

import java.io.Serializable; 

    public class Account implements Serializable 
    { 
private int MONEY; 
private String NAME; 
private String PASSWORD; 

public Account(Account acc) 
{ 
    MONEY = acc.getMoney(); 
    NAME = acc.getName(); 
    PASSWORD = acc.getPassword(); 
} 
private String getPassword() 
{ 
    return PASSWORD; 
} 
public Account(String n,String p,int m) 
{ 

    MONEY = m; 
    NAME =n; 
    PASSWORD = p; 
} 

public void setMoney(int m) 
{ 
    MONEY = m; 
} 
public void setName(String n) 
{ 
    NAME=n; 
} 
public void deposit(int m) 
{ 
    MONEY +=m; 
} 
public void withdraw(int m) throws NotEnoughMoneyException 
{ 
    if(MONEY-m<0) 
     throw new NotEnoughMoneyException(); 
    else 
     MONEY -=m; 
} 

public int getMoney() 
{ 
    return MONEY; 

} 
public String getName() 
{ 
    return NAME; 

} 

public String toString() 
{ 
    String s = getName()+":"+getMoney(); 
    return s; 
} 

    } 

    class NotEnoughMoneyException extends Exception 
    { 
String msg; 

NotEnoughMoneyException() 
{ 
    msg = "Not enough Money"; 
} 
    } 

回答

0

不知道你輸入到System.in中,你輸入的值超出了數組的範圍。只要仔細檢查一下,你是否意識到ArrayList從索引0開始吧?所以如果你的ArrayList中有5個對象,你的ArrayList索引爲0-4。

+0

它真的沒有功課,我只是用我自己的方式練習JAVA,試圖領先一步。 由System.in輸入您是在談論ArrayList 或類似的東西? – nasir 2012-03-28 02:12:59

+0

當您嘗試訪問第22行的userList作爲異常狀態時,您的indexOutOfBoundsException發生。你在那裏輸入什麼值?你正在放入一個超出數組本身大小的值。 – DavidB 2012-03-28 02:33:36