How to iterate through a textfile?
I have a text file with this information:
2
Orc
name = Gnorck
experience = 20
maxHealthPoints = 50
healthPoints = 30
maxBerserk = 9
berserk = 7
Human
name = Aragorn
experience = 45
maxHealthPoints = 40
healthPoints = 30
maxGreed = 20
greed = 10
Now I need a method that finds the strings Human and Orc every 7 lines.
I think I can just create a for loop and check with nextLine constantly if the string matches Orc or Human. But what do I do when I have the classes human and orc and want to create the character with the followed attributes?
So let's say I'm searching for Orcs. Whenever I find an Orc string in the file I want it to create the actual Orc with the attributes below.
I don't want you guys to write a whole script (not that you would do that). I just need a push in the right direction. Is it possible to grab like groups of 7 lines out of the file for every orc it finds and then sends that info the orc constructor?
Thanks!
[1072 byte] By [
Roekemoesa] at [2007-10-3 10:33:54]

Use xml
<character>
Orc
<name>Gnorck</name>
<experience>20</experience>
<maxHealthPoints>50</maxHealthPoints>
<healthPoints>30</healthPoints>
<maxBerserk>9</maxBerserk>
<berserk>7</berserk>
</character>
<character>
Human
<name>Aragorn</name>
<experience>45</experience>
<maxHealthPoints>40</maxHealthPoints>
<healthPoints>30</healthPoints>
<maxGreed>20</maxGreed>
<greed>10</greed>
</character>
Then it's almost trivial to use xpath to parse the data out and pass it to a Character constructor.
Ok I wrote some code to iterate through the file:
package RPGApplicatie;
import java.util.*;
public class Karakters {
//Constructor
public Karakters(int size){
karakters = new ArrayList<Karakter>(size);
}
//Methode voor het toevoegen van een karakter
public static void voegToe(Karakter k){
karakters.add(k);
}
//Zoeken naar een Orc of Human
public static Karakters readFile(String filename){
SimpleInput file = new SimpleInput(filename);
int size = file.nextInt();
Karakters charList = new Karakters(size);
for(int i = 0; i<(7*size); i++){
String line = file.nextLine();
if(line.equals("Human")){
String name = file.nextLine();
Karakter human1 = new Human(name);
Karakters.voegToe(human1);
}
if(line.equals("Orc")){
String name = file.nextLine();
Karakter orc1 = new Orc(name);
Karakters.voegToe(orc1);
}
}
return charList;
}
public String toString(){
String list = "Lijst van karakters: ";
for(int i = 0; i < karakters.size(); i++){
list.concat(karakters.get(i).toString());
}
return list;
}
//Variabelen aanmaken
private static ArrayList<Karakter> karakters;
Now I try to run it with this:
package RPGApplicatie;
public class RPGApplicatie {
public static void main(String[] args) throws ClassNotFoundException, RuntimeException, InstantiationException, IllegalAccessException {
Karakters test = Karakters.readFile("C:\\Documents and Settings\\Sander\\workspace\\Practicum 6a\\RPGApplicatie\\test.txt");
System.out.println(test.toString());
}
And this is the error I get:
Exception in thread "main" java.lang.RuntimeException: End of input.
at RPGApplicatie.SimpleInput.nextLine(SimpleInput.java:225)
at RPGApplicatie.Karakters.readFile(Karakters.java:21)
at RPGApplicatie.RPGApplicatie.main(RPGApplicatie.java:5)
I'm at the point that I know it tries to read a line that's not there. The SimpleInput does have a close() method, where should I put this one?