Home > Back-end >  How to get both word list and number of words of a text file in java?
How to get both word list and number of words of a text file in java?

Time:10-11

I'm new here and I want to get both word list and number of words of a text file. this is the code I have tried:

public class Main {

    public static void main(String args[]) throws Exception {
        // write your code here
        String line;


        FileReader fr = new FileReader("Wordlist (1).txt");
        BufferedReader br = new BufferedReader(fr);

        int i;
        while ((i = br.read()) != -1) {
            System.out.print((char) i);
        }

        int count = 0;

        if ((line = br.readLine()) == null) {
            System.out.println("Error");
        } else {
            //Splits each line into words
            String words[] = line.split("\\s ");
            //Counts each word
            count = count   words.length;

        }
        //Gets each line till end of file is reached
        /*while ((line = br.readLine()) != null) {
            //Splits each line into words
            String words[] = line.split("\\s ");
            //Counts each word
            count = count   words.length;

        }*/

        System.out.println("Number of words in the file: "   count);
        br.close();
        fr.close();
    }
}

Output: century Cepheus CEQ ceramic ceramium Cerberus cereal cerebellum Error Number of words in the file: 0

CodePudding user response:

To add the words in the line method Collections.addAll may be used as shown below.

Also, since Java 7 it is recommended to use try-with-resources to ensure the input/output streams are closed properly

List<String> words = new ArrayList<>();

try (
    BufferedReader br = new BufferedReader(
        new FileReader("Wordlist (1).txt")
    )
) {
    String line;

    while ((line = br.readLine()) != null) {
        Collections.addAll(words, line.split("\\s "));
    }
}

System.out.println(words);
System.out.println("Number of words in the file: "   words.size());

if "new" I/O and Java Stream API (since Java 8) is used, the words may be collected to the list as follows:

List<String> words = Files.lines(Path.of("Wordlist (1).txt")) // Stream<String> of lines
    .flatMap(line -> Arrays.stream(line.split("\\s "))) // Stream<String> of words
    .collect(Collectors.toList());

System.out.println(words);
System.out.println("Number of words in the file: "   words.size());

CodePudding user response:

delete these code:

int i;
while ((i = br.read()) != -1) {
    System.out.print((char) i);
}

cuz after br.read() finished, the pointer will point the end of contents of the file. so if you then call br.readLine() as follows, it will typically return -1, which means you have already finish reading the whole file.

so you have to delete the code above and then use the code below:

int count = 0;
while ((line = br.readLine()) != null) {
    //Splits each line into words
    String words[] = line.split("\\s ");
    //Counts each word
    count = count   words.length;
}
  • Related