私のクラスのすべてがseriazableを実装しているnotSeriazableを解決するのに問題がありましたが、それについて常にエラーが発生します。
java.io.NotSerializableException: java.util.Scanner
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Assign3.main(Assign3.java:119)
スキャナーはクラスです。意味がわかりません..スキャナーもシリアル化する必要があると思いますか?? しかし、スキャナークラスには触れられません。
Bank b = new Bank(100.0);
try{
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("try.txt"));
save.writeObject(b);
save.close();
}catch(Exception ex){ex.printStackTrace();}
import java.io.Serializable;
import java.util.Scanner;
public class Bank implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int Maxtransaction = 1000;
public Transaction[] transactions = new Transaction[Maxtransaction];
private int numTransactions = 0;
private double balance;
Scanner sn = new Scanner(System.in);
public Bank()
{
}
public Bank(double balance)
{
this.balance = balance;
}
public double getBalance()
{
return balance;
}
public void display()
{
System.out.print("\nTRANSACTION RECORD:\n");
for(int i = 0; i<numTransactions;i++)
{
System.out.print(transactions[i].toString()+"\n");
}
System.out.print("BALANCE : $"+getBalance()+"\n");
}
public boolean addTransaction(Category categories)
{
if(numTransactions>Maxtransaction){return false;}
else{
String[] chooseaccountletter = {"c","d"};
String chooseaccount;
boolean erroraccount = true;
System.out.print("\n");
do{
System.out.print("Enter a c for credit or d for debit: ");
chooseaccount = sn.nextLine();
for(int i = 0 ; i<chooseaccountletter.length;i++)
{
if(chooseaccountletter[i].equals(chooseaccount))
{
erroraccount = false;
break;
}
}
}while(erroraccount);
if(chooseaccountletter[0].equals(chooseaccount))
{
Transaction credit = new CreditTransaction();
credit.getTransactionInfo(categories);
credit.getAmout();
balance += credit.getAmout();
transactions[numTransactions] = credit;
}//end credit
if(chooseaccountletter[1].equals(chooseaccount))
{
Transaction debit = new DebitTransaction();
debit.getTransactionInfo(categories);
debit.getAmout();
balance += debit.getAmout();
transactions[numTransactions] = debit;
}//end debit
numTransactions++;
return true;
}
}
public void displayCategory(String Category)
{
double total = 0;
System.out.print("\nTransactions for Category "+Category+" :\n");
for(int i = 0; i<numTransactions;i++){
if(transactions[i].getCategory() == Category)
{
System.out.print(transactions[i].toString()+"\n");
total += transactions[i].getAmout();
}
}
System.out.print("Category Balance : $"+total+"\n");
}
}