Reading contents for a text file and couting words

J

jazzy

Hi,
I am trying to write a Java program to read a text file and then count the
number of words in that file. I keep on getting error.
Thanks

-JM
Here is the sample code.


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

public class ReadFile {
public static void main(String[] args)

{
//ReadFile Myfile.txt
int totalWords = 0;
String line;
String word;
// Open and Read the Myfile.txt

try {
BufferedReader in = new BufferedReader(new
FileReader("Myfile.txt"));
String str;
while ((str = in.readLine()) != null) {
//Process string
}
in.close();
} catch (IOException e) {

}
StringTokenizer str = new StringTokenizer(line, " \t\n\r");
while(str.hasMoreTokens())
{
totalWords++;
word = str.nextToken();
}
}
}
 
K

Kevin C.

jazzy said:
Hi,
I am trying to write a Java program to read a text file and then count the
number of words in that file. I keep on getting error.
Thanks

_What_ error?
 
A

Andrew Thompson

_What_ error?

That is a very good question Kevin.
<http://www.physci.org/codes/javafaq.jsp#exact>

To the OP, try this compileable variant of
your code and you may begin to understand..

<sscce>
import java.util.*;
import java.lang.*;
import java.io.*;

public class ReadFile {

public static void main(String[] args) {
//ReadFile Myfile.txt
int totalWords = 0;
String line = "";
String word;
// Open and Read the Myfile.txt

try {
BufferedReader in = new BufferedReader(new
FileReader("Myfile.txt"));
String str;
while ((str = in.readLine()) != null) {
//Process string
}
in.close();

StringTokenizer st = new StringTokenizer(line, " \t\n\r");
while(st.hasMoreTokens()) {
totalWords++;
word = st.nextToken();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
</sscce>

Also have a look over this..
<http://www.physci.org/codes/sscce.jsp>
(your code did not compile)

HTH
 
G

Gordon Beaton

I am trying to write a Java program to read a text file and then
count the number of words in that file. I keep on getting error.

Exactly *what* error do you get?
import java.util.*;
import java.lang.*;

It is never necessary to import classes from java.lang.
import java.io.*;

public class ReadFile {
public static void main(String[] args)
{
//ReadFile Myfile.txt
int totalWords = 0;
String line;
String word;
// Open and Read the Myfile.txt

try {
BufferedReader in = new BufferedReader(new
FileReader("Myfile.txt"));

Suggestion: get the filename from the command line. Do you intend to
edit and recompile the program each time you want to count the words
in a different file?
String str;
while ((str = in.readLine()) != null) {
//Process string

You forgot to actually process the String here. Perhaps you need to
count the words on this line, before continuing with the next one.
}
in.close();
} catch (IOException e) {

It's a good idea to either handle or report this exception if it
occurs, not just hide it (which the empty catch block does).

I suspect this is the String processing code you had intended to
invoke in the above loop. You can either move it there, or make it
into a separate method, which you call from there:
StringTokenizer str = new StringTokenizer(line, " \t\n\r");
while(str.hasMoreTokens())
{
totalWords++;
word = str.nextToken();
}
}
}

You might also be interested to know that StringTokenizer can count
the tokens for you, you don't need to iterate over them yourself.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top