Skip to main content

File Reader....reached end of file while parsing ???

1 reply [Last post]
sooz719
Offline
Joined: 2007-04-16

I need to create a class that reads a text file of the following format:

@card
{
name = "Vicky Pollard",
age = "14",
smelliness = "47",
warts = "16",
beauty = "2",
weight = "14"
}

A card is made up of the above values. I want the file to read in these values and store them in an array made up of card objects. I have composed the following code, but it creates the error : reached end of file while parsing.

import java.io.*;
import java.util.*;

public class CardReader2
{

Card[] aCard = new Card[30];
int x;
public String s = new String();
public String name = new String();
public String age = new String();
public String smelliness = new String();
public String warts = new String();
public String beauty = new String();
public String weight = new String();
Integer testForInt;
StreamTokenizer stream = null;

public static void main(String[] args) throws Exception
{
try {
stream = new StreamTokenizer (new FileReader("Card.txt"));
int token = st.nextToken();

while (token != StreamTokenizer.TT_EOF) {

token = st.nextToken();

if (token == StreamTokenizer.TT_WORD) {
s = st.sval;

if (s.equals("name")) {
st.nextToken();
st.nextToken();
name = st.sval;
}

if (s.equals("age")) {
st.nextToken();
st.nextToken();
age = st.sval;
if (age != null)
testForInt = new Integer(age);
}

if (s.equals("smelliness")) {
st.nextToken();
st.nextToken();
smelliness = st.sval;
if (smelliness != null)
testForInt = new Integer(smelliness);
}

if (s.equals("warts")) {
st.nextToken();
st.nextToken();
warts = st.sval;
if (warts != null)
testForInt = new Integer(warts);
}

if (s.equals("beauty")) {
st.nextToken();
st.nextToken();
beauty = st.sval;
if (beauty != null)
testForInt = new Integer(beauty);
}

if (s.equals("weight")) {
st.nextToken();
st.nextToken();
weight = st.sval;
if (weight != null)
testForInt = new Integer(weight);
}
}

Character ch = new Character((char)st.ttype);
s=ch.toString();

if(s.equals("}")) {

aCard[x] = new Card(name, age, smelliness, warts, beauty, weight);

name = null;
age = null;
smelliness = null;
warts = null;
beauty = null;
weight = null;
x = x+1;
}

}
}

catch(NumberFormatException error) {
System.out.println("There is an error in the top trumps database");
cardArray.clear();
}

catch(FileNotFoundException error) {
System.out.println("There are no top trumps to play with");
cardArray.clear();
}

}

Can someone please help me fix this?

Thanks in advance for any help

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

[code]if(s.equals("}")) {[/code]

This section is never reached. The braces are not word characters in the StreamTokenizer's default dictionary. The [i]ttype[/i] field holds a single character when one is encountered, so your overall structure should be along the lines of:

[code]String s;
for (int token = st.nextToken(); token != StreamTokenizer.TT_EOF; token = st.nextToken()) {
switch (token) {
case TT_WORD:
s = st.sval;
if (s.equals("age")) {
// ...
} else if (s.equals("smelliness")) {
// ...
} // ...
break;

case '}':
// Done with this context
aCard[x] = new Card(name, age, smelliness, warts, beauty, weight);
// Do whatever cleanup is needed
break;

// Handle other special characters, such as '@', '=', or '{'
}
}[/code]

This should solve your EOF issues.