我已经用Google搜索了这个主题好几天了,找不到我的问题的答案。在我的" tryReadClub"方法,我得到代码行的classcastexception
" retrieveClubID =(ClubInfo)in.readObject();"
我非常喜欢新手,我一直在阅读有关施法的内容,而我的理解是我正在尝试进行不兼容的类型转换,但我对修复它感到困惑。 " ClubInfo"是我的一个类的名称(见下文)," retrieveClubID"只是一个对象引用,指向一个无参数的构造函数。
如果有人能澄清我做错了什么,我将非常感激。此外,任何人都可以建议一个网站,它将澄清"文件/缓冲/输出"之间的差异。流?我正在努力寻找一个能够深入探讨这些流程的网站。
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class ReadClub implements java.io.Serializable
{
private static final long serialVersionUID = 1;
public ReadClub()
{
}
public void tryReadClub(){
ClubInfo retrieveClubID = new ClubInfo();
ClubInfo retrieveClubName = new ClubInfo();
System.out.println("Club ID before is : " + retrieveClubID.getClubID());
System.out.println("Currently printing blah : " + retrieveClubID);
String fileName = "/home/cabox/workspace/savedgames/" + retrieveClubID.getClubID();
try
{
FileInputStream fileIn = new FileInputStream("" + fileName + ".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
retrieveClubID = (ClubInfo) in.readObject();
// retrieveClubName = (ClubInfo) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
System.out.println("Not a valid number");
return;
}catch(ClassNotFoundException c)
{
System.out.println("Club class not found");
c.printStackTrace();
return;
}
System.out.println("Saved game loaded...");
System.out.println("Club Name: " + retrieveClubName.getClubInfo());
System.out.println("Club ID: " + retrieveClubID.getClubID());
}
}
public class ClubInfo implements java.io.Serializable
{
private static final String pound = "GBP";
private double startingBalance;
private double currentBalance;
private int clubID = 1;
private static int numberOfClubIDS;
private String clubTest;
private static final int[] originalStadiumCapacity = new int[21];
private static final String[][] club = new String[20][2]; // COLUMN 0 IS CLUBNAME, COLUMN 1 IS STADIUM NAME
private String division;
private transient int SSN;
public ClubInfo()
{
}
public ClubInfo(int clubID)
{
this.clubID = clubID;
}
public ClubInfo(double startingBalance , double currentBalance , String division)
{
this.startingBalance = startingBalance;
this.currentBalance = currentBalance;
this.division = division;
}
public String toString()
{
return clubTest;
}
public int getClubID()
{
return clubID;
}
public String[][] getClubInfo()
{
club[0][0] = "Arsenal"; club[0][1] = "Emirates";
club[1][0] = "Aston Villa"; club[1][1] = "Villa Park";
club[2][0] = "Bournemouth"; club[2][1] = "Blah";
club[3][0] = "Chelsea"; club[3][1] = "Blah";
club[4][0] = "Crystal Palace"; club[4][1] = "Blah";
club[5][0] = "Everton"; club[5][1] = "Blah";
club[6][0] = "Leicester City"; club[6][1] = "Blah";
club[7][0] = "Liverpool"; club[7][1] = "Blah";
club[8][0] = "Manchester United"; club[8][1] = "Blah";
club[9][0] = "Manchester City"; club[9][1] = "Blah";
club[10][0] = "Newcastle United"; club[10][1] = "Blah";
club[11][0] = "Norwich City"; club[11][1] = "Blah";
club[12][0] = "Southampton"; club[12][1] = "Blah";
club[13][0] = "Stoke City"; club[13][1] = "Blah";
club[14][0] = "Sunderland"; club[14][1] = "Blah";
club[15][0] = "Swansea City"; club[15][1] = "Blah";
club[16][0] = "Tottenham Hotspur"; club[16][1] = "Blah";
club[17][0] = "Watford"; club[17][1] = "Blah";
club[18][0] = "West Brom"; club[18][1] = "Blah";
club[19][0] = "West Ham United"; club[19][1] = "Blah";
return club;
}
public int[] getOriginalStadiumCapacity()
{
originalStadiumCapacity[0] = 70000;
originalStadiumCapacity[1] = 30000;
originalStadiumCapacity[2] = 25000;
originalStadiumCapacity[3] = 50000;
originalStadiumCapacity[4] = 20000;
originalStadiumCapacity[5] = 40000;
originalStadiumCapacity[6] = 30000;
originalStadiumCapacity[7] = 50000;
originalStadiumCapacity[8] = 80000;
originalStadiumCapacity[9] = 40000;
originalStadiumCapacity[10] = 70000;
originalStadiumCapacity[11] = 20000;
originalStadiumCapacity[12] = 30000;
originalStadiumCapacity[13] = 30000;
originalStadiumCapacity[14] = 40000;
originalStadiumCapacity[15] = 30000;
originalStadiumCapacity[16] = 60000;
originalStadiumCapacity[17] = 20000;
originalStadiumCapacity[18] = 30000;
originalStadiumCapacity[19] = 40000;
return originalStadiumCapacity;
}
}