Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Keim

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

This topic is 5996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Yes, code would really help! There are a few ways to take in input from a file, so it seeing your code is important.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
...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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!