Need help with java program?

by 3 replies
4
I'm trying to create a program that reads a text file and count the number of alphabetic words in the file. I can't get my program to read just the alphabetic words. Here is my program
import java.util.*;
import java.io.*;

public class MyFileContents7 {
public static void main (String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("address.txt"));
int lines = 0;
int words = 0;
while (reader.readLine() != null){
lines++;
}

File file = new File("address.txt");
Scanner sc = new Scanner(new FileInputStream(file));
int count=0;
while(sc.hasNext()){
sc.next();
count++;
}
System.out.println("Number of words: " + count);



System.out.println(lines);
reader.close();
}
}
#programming #java #program
  • What other types of words are there?
    • [1] reply
    • You should use stringtokenizer for your problem it will give you exact number of words in the file when you read file line by line your code is correct but use stringtokenizer
  • You should append your file's content to a StringBuilder object and generate tokens from it via StringTokenizer.

Next Topics on Trending Feed

  • 4

    I'm trying to create a program that reads a text file and count the number of alphabetic words in the file. I can't get my program to read just the alphabetic words. Here is my program import java.util.*; import java.io.*;