Reading from a text file

I'm attempting to read in a from the following from a txt file and prints out the first line and all the lines that begins with an attacker, until it gets to a a defender win or attacker win and then prints the rest of the file. However st is taking both the Attacker and part of Name, which is somehow throwing a NoSuchElementException. I only want it to take the Attacker, not part of the Name. I can't figure out why it's doing this and how to correct it.

Text File:

Attacker Name Here

Weapons:+10% Shields:+10% Armour:+10%

TypeS.CargoL.CargoL.FighterH.FighterCruiserB.ShipCol. ShipRecy.Esp. ProbeBomberDest.RipBattlecr

Num1111111111111

Weapon55551654401.10055101.1002.200220.000770

Shields112711275522011011055055055.000440

Armour4401.3204401.1002.9706.6003.3001.7601108.25012.100990.0007.700

Defender Name Here

Weapons:+10% Shields:+10% Armour:+10%

TypeS.CargoL.CargoL.FighterH.FighterCruiserB.ShipCol. ShipRecy.Esp. ProbeBomberSol. SatDest.RipBattlecrMiss.L.LaserH.LaserGaussIon.CPlasmaS.DomeLS.Dome

Num1111111111111111111111

Weapon55551654401.10055101.10012.200220.000770881102751.2101653.30011

Shields1127112755220110110550155055.00044022271102205503302.20011.000

Armour4401.3204401.1002.9706.6003.3001.7601108.25022012.100990.0007.7002202208803.85088011.0002.20011.000

The attacking fleet fires a total of 18 times with the total power of 1.059.543 upon the defender.

The defender's shields absorb 18.910 damage points.

The defending fleet fires a total of 33 times with the total power of 1.922.677 upon the attacker.

The attacker's shields absorb 38.730 damage points.

Attacker 1

TypeB.ShipCol. ShipRip

Num001

Weapon1.10055220.000

Shields22011055.000

Armour6.6003.300990.000

Defender 1

TypeS.CargoL.CargoL.FighterH.FighterCruiserB.ShipCol. ShipEsp. ProbeBomberDest.RipBattlecrMiss.L.LaserIon.CS.DomeLS.Dome

Num00000000001000000

Weapon55551654401.1005501.1002.200220.0007708811016511

Shields1127112755220110055055055.00044022275502.20011.000

Armour4401.3204401.1002.9706.6003.3001108.25012.100990.0007.7002202208802.20011.000

The attacking fleet fires a total of 11 times with the total power of 2.276.933 upon the defender.

The defender's shields absorb 17.510 damage points.

The defending fleet fires a total of 47 times with the total power of 9.540.458 upon the attacker.

The attacker's shields absorb 15.761 damage points.

Attacker 1

TypeRip

Num0

Weapon220.000

Shields55.000

Armour990.000

Defender 1

TypeS.CargoB.ShipEsp. ProbeBomberDest.RipBattlecrS.DomeLS.Dome

Num000000000

Weapon51.10001.1002.200220.00077011

Shields11220055055055.0004402.20011.000

Armour4406.6001108.25012.100990.0007.7002.20011.000

The battle ends draw.

The attacker has lost a total of 10.178.000 units.

The defender has lost a total of 1.836.800 units.

At these space coordinates now float 1.776.210 Metal and 1.413.954 Crystal.

Code:

try

{

Scanner Reader =new Scanner(new File("CR.txt"));//Reads in the file.

//Reader.hasNext();

System.out.println(Reader.nextLine());

Reader.nextLine();

//System.out.println(Reader.nextLine());

//System.out.println(Reader.nextLine());

while(Reader.hasNext() && flag!=1)

{

line=Reader.nextLine();//Separates the file

StringTokenizer st =new StringTokenizer(line," ");

st.nextToken().trim();

if("Attacker".equals(st))

{

System.out.println(line);

}

if("The defender has won the battle!".equals(line) ||"The attacker has won the battle!".equals(line))

{

temp=line;

flag=1;

}

}

System.out.println();

System.out.println();

System.out.println();

System.out.println(temp);

while(Reader.hasNext())

{

line=Reader.nextLine();

System.out.println(line);

}

}

catch (FileNotFoundException e)

{

System.out.println("I/O Error, FileNotFound");

}

catch (NoSuchElementException e)

{

System.out.println("I/O Error");

}

[5398 byte] By [davegelha] at [2007-11-27 4:55:28]
# 1
Okay after some more debugging I realized the exception is being thrown when it comes to a blank line. However I'd still like help figuring out how to correct the st problem and any suggestions as to how make it not throw an exception when it reads a blank line.
davegelha at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 2
Anyone going to help?
davegelha at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 3

> Anyone going to help?

Don't be impatient. We offer our help free of charge and are under no obligation to help you at all.

I have a question. Do you plan on expanding the program later and the need to tokenize the line will be used then? If not just use startsWith to see if the line starts with "Attacker" instead of needelessly tokenizing the line and then getting the first token.

floundera at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you flounder. The startsWith works, now to figure out how to get it to not through an exception when it hits a blank line. I do plan on extending program and use a tokenizer to break up the lines that I want to copy but I can do it once I get the program to read in from the lines I want from the text file.

Also the reason I got impatient is becuase the post fell into the second page and no one had answered it, from my experience in this forum and other forums a post that falls to the second page rarely ever get read and/or responded to. So I apologize.

davegelha at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 5
If you are having trouble with a blank line then chuck in an if statement.if(line.length() > 0) {// your code}
floundera at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks but once I took the tokenizer out the error stop getting thrown, so now back to working on the program.
davegelha at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...
# 7

st.nextToken().trim();

if("Attacker".equals(st))

Probably had something to do with one or both of these two lines. You are basically throwing away the first token in the tokenizer as you do not assign it to anything. Secondly you are trying to compare(using the equals method) a String and a StringTokenizer.

floundera at 2007-7-12 10:10:26 > top of Java-index,Java Essentials,New To Java...