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]
# 1
Is this related to? http://forum.java.sun.com/thread.jspa?threadID=788349&messageID=4479605#4479605
zadoka at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 2
Once you find the Orc heading, create a new Orc. Then search for name, and set his name to that. Just keep doing that until youve gone through everything.
CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 3

@zadok

Well it is related in the way that it's the same program I'm trying to built. It's not that I'm asking the same question twice. I'm having serious doubts about that first post since I don't think the forName() method is suited for this. I've gone in another direction and had a new question, that's why I posted a new topic.

@CaptainMorgan

Looks ok. About that though. I'm using SimpleInput. Once I find the name and I create it. Can I use nextLine() for the name? Or would it catch the wrong line?

Thanks

Message was edited by:

Roekemoes

Roekemoesa at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 4
Ive never heard of SimpleInput. Unless you need to use it, use Scanner.
CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 5

Alright sorry about that. I suppose Scanner is roughly the same. It all has methods like nextLine and nextInt etc.

I just wondered where java continued after constructing the new object and then calling nextLine() again.

I'll try to write the code and post an update soon.

Thanks!

Roekemoesa at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 6

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.

hunter9000a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 7
> I just wondered where java continued after> constructing the new object and then calling> nextLine() again.You could just read all of the lines in the text file into an ArrayList, then search through the ArrayList whenever you need to.
CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 8
That's not a bad idea but could I still resolve integers out of those then?I could add this to an ArrayList for example: experience = 45But that would be a string then right?
Roekemoesa at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 9
> I could add this to an ArrayList for example:> experience = 45> But that would be a string then right?Yes, that would be a String, but it wouldnt be too hard to find the number in there.
CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 10

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?

Roekemoesa at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 11

Use Scanner! Look at your modified method:

public static ArrayList<String> readFile(String filename){

Scanner file = new Scanner(filename);

ArrayList<String> fileInfo = new ArrayList<String>();

while(file.hasNextLine()){

fileInfo.add(file.nextLine());

}

return fileInfo;

}

CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 12
Thanks I'll try this later tonight.So basically I put every line in an array list. And then I know a pattern of where my attributes, name etc. are. Thanks for the help and if I have time I'll post an update.Thanks again.
Roekemoesa at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...
# 13
I would use that method just for getting all the information into the ArrayList. You can create another method to do stuff with it.
CaptainMorgan08a at 2007-7-15 5:57:07 > top of Java-index,Java Essentials,New To Java...