Data not being read from file
Sorry to be asking you guys so many questions but I guess with this project I bit off a bit more than I could chew...
I'm trying to read data from a dat file and then putting it into an array to display. However it just won't read it into the array for some reason. I'd really appreciate any help!
Here is the method which calls for the array to be made:
publicboolean checkForFile(){
try{
FileReader theFile =new FileReader("HeroDatabase.dat");
BufferedReader inFile =new BufferedReader(theFile);
Hero root =new Hero();
LoadSave temp =new LoadSave(root);
int records = temp.countRecords();
Hero[] tempArray =new Hero[records];
DotaDatabase app =new DotaDatabase();
tempArray = app.retrieveDatabase();
heroList.setListData(tempArray);
inFile.close();
JetsIO.output("The file has been loaded for viewing.");
returntrue;
}
catch(Exception io){
heroDetails.setText("Error trying to open file " + io.getMessage());
HeroViewer.this.setVisible(false );
HeroViewer.this.dispose();
GUI menu =new GUI();//helper method
}
returnfalse;
}
And this is the method which actually creates the array:
public Hero[] retrieveDatabase(){
Hero root =new Hero();
LoadSave temp =new LoadSave(root);
Hero[] tempArray =new Hero[temp.countRecords()];
for (int x = 0; x < temp.countRecords(); x++){
String name = temp.readName(x);
String heroClass = temp.readHeroClass(x);
String primaryAttribute = temp.readPrimaryAttribute(x);
String heroType = temp.readHeroType(x);
String affiliation = temp.readAffiliation(x);
int armour = temp.readArmour(x);
int strength = temp.readStrength(x);
int agility = temp.readAgility(x);
int intelligence = temp.readIntelligence(x);
double strengthGainPerLevel = temp.readStrengthGainPerLevel(x);
double agilityGainPerLevel = temp.readAgilityGainPerLevel(x);
double intelligenceGainPerLevel = temp.readIntelligenceGainPerLevel(x);
int damage = temp.readDamage(x);
int attackRange = temp.readAttackRange(x);
String skill1 = temp.readSkill1(x);
String skill2 = temp.readSkill2(x);
String skill3 = temp.readSkill3(x);
String ultimateSkill = temp.readUltimateSkill(x);
String description = temp.readDescription(x);
Hero toBeLoaded =new Hero();
toBeLoaded.setName(name);
toBeLoaded.setHeroClass(heroClass);
toBeLoaded.setPrimaryAttribute(primaryAttribute);
toBeLoaded.setType(heroType);
toBeLoaded.setAffiliation(affiliation);
toBeLoaded.setArmour(armour);
toBeLoaded.setStrength(strength);
toBeLoaded.setAgility(agility);
toBeLoaded.setIntelligence(intelligence);
toBeLoaded.setStrengthGainPerLevel(strengthGainPerLevel);
toBeLoaded.setAgilityGainPerLevel(agilityGainPerLevel);
toBeLoaded.setIntelligenceGainPerLevel(intelligenceGainPerLevel);
toBeLoaded.setDamage(damage);
toBeLoaded.setAttackRange(attackRange);
toBeLoaded.setSkill1(skill1);
toBeLoaded.setSkill2(skill2);
toBeLoaded.setSkill3(skill3);
toBeLoaded.setUltimateSkill(ultimateSkill);
toBeLoaded.setDescription(description);
tempArray[x] = toBeLoaded;
}
return tempArray;
}
I don't know why it won't display the array, it appears to have loaded the file and entered it into an array but just can't display the array.
Gah, it won't let me update my above post, but here is the code with a couple of additions, but still the same problem:
public boolean checkForFile(){
try{
FileReader theFile = new FileReader("HeroDatabase.dat");
BufferedReader inFile = new BufferedReader(theFile);
Hero root = new Hero();
LoadSave temp = new LoadSave(root);
int records = temp.countRecords();
Hero[] tempArray = new Hero[records];
DotaDatabase app = new DotaDatabase();
tempArray = app.retrieveDatabase();
heroList.setListData(tempArray);
inFile.close();
JetsIO.output("The file has been loaded successfully.");
return true;
}
catch(Exception io){
heroDetails.setText("Error trying to open file " + io.getMessage());
HeroViewer.this.setVisible( false );
HeroViewer.this.dispose();
}
return false;
}
/**
* retrieveDatabase method to get the data from file on disk and insert it into the binary tree in RAM
* Utilises FOR loops
* Utilises binary selection in IF loops
* @return Hero[] tempArray, array of Hero objects for display
*/
public Hero[] retrieveDatabase(){
Hero root = new Hero();
Hero toBeLoaded = new Hero();
LoadSave temp = new LoadSave(root);
int records = temp.countRecords();
Hero[] tempArray = new Hero[records];//array declaration for storing of records
for (int x = 0; x < records; x++){
boolean deleted = temp.readDeleted(x);
if (deleted = false){//checking whether the record has been flagged as deleted (ignored)
//reading attribute data from the file into variables
String name = temp.readName(x);
String heroClass = temp.readHeroClass(x);
String primaryAttribute = temp.readPrimaryAttribute(x);
String heroType = temp.readHeroType(x);
String affiliation = temp.readAffiliation(x);
int armour = temp.readArmour(x);
int strength = temp.readStrength(x);
int agility = temp.readAgility(x);
int intelligence = temp.readIntelligence(x);
double strengthGainPerLevel = temp.readStrengthGainPerLevel(x);
double agilityGainPerLevel = temp.readAgilityGainPerLevel(x);
double intelligenceGainPerLevel = temp.readIntelligenceGainPerLevel(x);
int damage = temp.readDamage(x);
int attackRange = temp.readAttackRange(x);
String skill1 = temp.readSkill1(x);
String skill2 = temp.readSkill2(x);
String skill3 = temp.readSkill3(x);
String ultimateSkill = temp.readUltimateSkill(x);
String description = temp.readDescription(x);
//putting the data into a Hero object for ease of transportation
toBeLoaded.setName(name);
toBeLoaded.setHeroClass(heroClass);
toBeLoaded.setPrimaryAttribute(primaryAttribute);
toBeLoaded.setType(heroType);
toBeLoaded.setAffiliation(affiliation);
toBeLoaded.setArmour(armour);
toBeLoaded.setStrength(strength);
toBeLoaded.setAgility(agility);
toBeLoaded.setIntelligence(intelligence);
toBeLoaded.setStrengthGainPerLevel(strengthGainPerLevel);
toBeLoaded.setAgilityGainPerLevel(agilityGainPerLevel);
toBeLoaded.setIntelligenceGainPerLevel(intelligenceGainPerLevel);
toBeLoaded.setDamage(damage);
toBeLoaded.setAttackRange(attackRange);
toBeLoaded.setSkill1(skill1);
toBeLoaded.setSkill2(skill2);
toBeLoaded.setSkill3(skill3);
toBeLoaded.setUltimateSkill(ultimateSkill);
toBeLoaded.setDescription(description);
toBeLoaded.setDeleted(deleted);
}//end if
tempArray[x] = toBeLoaded;//add a Hero object to the array
}//end for
return tempArray;//return the array full of Heroes
}//end retrieveDatabase method