Skip to main content
gamedev.net
🔒 Locked

[java] I'm having a file reading problem.

Started by Keim 5 replies 250+ views
Original Post
Keim
Keim
I was wondering if someone could help me out with a text file reading program. It''s supposed to read the file line by line, and print a set of tokens for each line. But it seems to only be outputting the first line of text. And yet at the point where it''s gathering the data it reads the entire 7 lines. I''ve been trying to debug it, but I just can''t figure it out. ICQ: 11451528 AIM: KhesioN I''d really appreciate the help. It''s getting frustrating. Thanks RMK
Ross K
HenryApe
HenryApe
Time to post some code me thinks.
fodd3r
fodd3r
Yes, code would really help! There are a few ways to take in input from a file, so it seeing your code is important.
Keim
Keim
Okay. I wasn''t sure if it was polite to post a lot of code in here. I''ll just post the method I believe I''m having the problem with:

public static void countWordOccurrence(Vector text, String fname)
{
int i, match = 0;
String line, word;
StringTokenizer tokenizer;

try
{
FileReader fr = new FileReader(fname);
BufferedReader inFile = new BufferedReader(fr);

line = inFile.readLine();
while(line != null)
{
tokenizer = new StringTokenizer(line);

while(tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
word = word.toLowerCase();

if( word.charAt(word.length()-1) == ''.'' ||
word.charAt(word.length()-1) == ''?'' ||
word.charAt(word.length()-1) == '','' ||
word.charAt(word.length()-1) == ''!'' )
{
word = word.substring(0, word.length()-1);
}

if(text.size() == 0)
{
text.addElement(new Data(word, 0));
}
else
{
for(i = 0; i < text.size(); i++)
{
if(((Data)text.elementAt(i)).str_type.equals(word))
{
((Data)text.elementAt(i)).data++;
match = 1;
}
}

if(match == 0)
{
text.addElement(new Data(word, 0));
}
}
}
System.out.println("\n<<< LINE END >>>");
line = inFile.readLine();
}
inFile.close();
}
catch(FileNotFoundException Exception)
{
System.out.println("Specified file could not be found.");
}
catch(IOException Exception)
{
System.out.println("Specified file could not be opened.");
}
}

RMK
Ross K
Keim
Keim
...it doesn''t like leading spaces I suppose. This might look better.

public static void countWordOccurrence(Vector text, String fname)
{
int i, match = 0;
String line, word;
StringTokenizer tokenizer;

try
{
FileReader fr = new FileReader(fname);
BufferedReader inFile = new BufferedReader(fr);

line = inFile.readLine();
while(line != null)
{
tokenizer = new StringTokenizer(line);

while(tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
word = word.toLowerCase();

if( word.charAt(word.length()-1) == ''.'' ||
word.charAt(word.length()-1) == ''?'' ||
word.charAt(word.length()-1) == '','' ||
word.charAt(word.length()-1) == ''!'' )
{
word = word.substring(0, word.length()-1);
}

if(text.size() == 0)
{
text.addElement(new Data(word, 0));
}
else
{
for(i = 0; i < text.size(); i++)
{
if(((Data)text.elementAt(i)).str_type.equals(word))
{
((Data)text.elementAt(i)).data++;
match = 1;
}
}

if(match == 0)
{
text.addElement(new Data(word, 0));
}
}
}
System.out.println("\n<<< LINE END >>>");
line = inFile.readLine();
}
inFile.close();
}
catch(FileNotFoundException Exception)
{
System.out.println("Specified file could not be found.");
}
catch(IOException Exception)
{
System.out.println("Specified file could not be opened.");
}
}

RMK
Ross K
Keim
Keim
I guess not.

RMK
Ross K
Keim
Keim
Nevermind. I just figured it out. I feel like an idiot.

I forgot to reset match to 0.

Why is it that you can look at something for so long...and then the answer pops up when you least expect it?

RMK
Ross K

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.