【发布时间】:2016-10-29 07:33:57
【问题描述】:
我收到了我发现的这个错误 - 尝试在对象数组中存储错误的类型。文件编译,我需要将正在读入的文件存储到 Animals[] 数组中。
这是整个文件:
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileInputStream;
public class Zoo
{
private Animals[] animals;
int count;
public final int Maximum_Count = 20;
public boolean constructed = false;
//Default Constructor
public Zoo()
{
count = 0;
animals = new Animals[Maximum_Count];
}
//Alternate Constructor
public Zoo(int inCount, Animals[] inAnimals)
{
if(validateCount(inCount))
{
animals = new Animals[Maximum_Count]; //Probably Wont have to validate Count
for(int ii = 0; ii < Maximum_Count; ii++)
{
inAnimals[ii] = new Animals(inAnimals[ii]);
}
}
}
//Copy Constructor
public Zoo(Zoo inZoo)
{
count = inZoo.getCount();
animals = inZoo.getAnimals();
}
//Mutators
public void setCount(int inCount)
{
if(validateCount(inCount))
{
count = inCount;
}
else
{
throw new IllegalArgumentException("Invalid Count");
}
}
public void setAnimals(Animals[] inAnimals)
{
if(inAnimals == null)
{
throw new IllegalArgumentException("Animals can not be found, array is empty.");
}
else
{
animals = new Animals[inAnimals.length];
for(int ii = 0; ii < inAnimals.length; ii++)
{
animals[ii] = new Animals(inAnimals[ii]);
}
}
}
public int getCount()
{
return count;
}
public Animals[] getAnimals()
{
Animals[] animalsCopy;
animalsCopy = new Animals[animals.length];
for(int ii = 0; ii < animals.length; ii++)
{
animalsCopy[ii] = new Animals(animals[ii]);
}
return animalsCopy;
}
public boolean equals(Object inObject)
{
Zoo inZoo;
boolean same = false;
if(inObject instanceof Zoo)
{
inZoo = (Zoo)inObject;
if(count == inZoo.getCount())
{
if(sameAs(animals,inZoo.getAnimals()))
{
same = true;
}
}
}
return same;
}
public String toString()
{
String outString = "Count: " + count;
for(int ii = 0; ii < animals.length; ii++)
{
outString = outString + ("Animals" + ii + ": " + animals[ii].toString());
}
return outString;
}
public void addFlying()
{
Scanner sc = new Scanner(System.in);
String species;
int numWings;
double mass;
System.out.println("Please enter the Species of the Flying Animal.");
species = sc.nextLine();
System.out.println("Please enter the Mass of the Flying Animal.");
mass = sc.nextDouble();
System.out.println("Please enter the Number of Wings the Flying Animal has.");
numWings = sc.nextInt();
Flying temp = new Flying(species,mass,numWings);
animals[count] = temp;
System.out.println("Animal has been added to the Zoo.");
count++;
}
public void addTerrestrial()
{
Scanner sc = new Scanner(System.in);
String species;
int numLegs;
double mass;
System.out.println("Please enter the Species of the Terrestrial Animal.");
species = sc.nextLine();
System.out.println("Please enter the Mass of the Terrestrial Animal.");
mass = sc.nextDouble();
System.out.println("Please enter the Number of Legs that the Terrestrial Animal has.");
numLegs = sc.nextInt();
Terrestrial temp = new Terrestrial(numLegs,mass,species);
animals[count] = temp;
System.out.println("Animal has been added to the Zoo.");
count++;
}
public void addAquatic()
{
Scanner sc = new Scanner(System.in);
String species;
int numFins;
double mass;
System.out.println("Please enter the Species of the Aquatic Animal.");
species = sc.nextLine();
System.out.println("Please enter the Mass of the Aquatic Animal.");
mass = sc.nextDouble();
System.out.println("Please enter the Number of Fins that the Aquatic Animal has.");
numFins = sc.nextInt();
Aquatic temp = new Aquatic(mass,species,numFins);
animals[count] = temp;
System.out.println("Animal has been added to the Zoo.");
count++;
}
public void displayAnimals()
{
for(int i=0; i<count; i++)
{
if(animals[i] instanceof Flying)
{
Flying test1 = new Flying((Flying)animals[i]);
System.out.println(test1.toString());
}
if(animals[i] instanceof Terrestrial)
{
Terrestrial test2 = new Terrestrial((Terrestrial)animals[i]);
System.out.println(test2.toString());
}
if(animals[i] instanceof Aquatic)
{
Aquatic test3 = new Aquatic((Aquatic)animals[i]);
System.out.println(test3.toString());
}
}
}
public Animals[] readFile()
{
try
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file name.");
String fileName = sc.nextLine();
String string;
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
List<String> list = new ArrayList<String>();
for(int a = 0; a<animals.length; a++)
{
while((string = bufferedReader.readLine()) != null)
{
list.add(string);
}
animals = list.toArray(new Animals[a]);
}
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
catch(ArrayStoreException e2)
{
System.out.println("Attempt to Store wrong type of object into an array of objects.");
}
return animals;
}
//Private Submodules
private boolean sameAs(Object[] array1, Object[] array2)
{
boolean sameAs = true;
if(array1.length != array2.length)
{
sameAs = false;
}
else
{
int count = 0;
do
{
sameAs = array1[count].equals(array2[count]);
count++;
}
while(sameAs && (count < array1.length));
}
return sameAs;
}
private boolean validateCount(int inCount)
{
return((inCount > 0) && (inCount <= 20));
}
}
这是我遇到问题的 readFile 子模块。其他一切正常,文件编译。 欢迎任何解决方案,我已经绞尽脑汁好几个小时了。
【问题讨论】: