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();
}
}
}
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();
}
}
}