Need help with java program?

3 replies
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();
}
}
#java #program
  • Profile picture of the author Valdor Kiebach
    Originally Posted by tementaXCD View Post

    I can't get my program to read just the alphabetic words
    What other types of words are there?
    {{ DiscussionBoard.errors[8245551].message }}
    • Profile picture of the author bugtrack
      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
      {{ DiscussionBoard.errors[8246805].message }}
  • Profile picture of the author Prrmod
    You should append your file's content to a StringBuilder object and generate tokens from it via StringTokenizer.
    {{ DiscussionBoard.errors[8249016].message }}

Trending Topics