简体   繁体   中英

Java word appearence in a text file

For the given text file (text.txt) compute how many times each word appears in the file. The output of the program should be another text file containing on each line a word and then the number of times it appears in the original file. After you finish change the program so that the words in the output file are sorted alphabetically. Do not use maps, use only basic arrays. The thing is displaying me only one word that I enter from keyboard in that text file, but how can I display for all words, not only for one? Thanks

package worddata;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

 class WordData {
     public FileReader fr = null;
     public BufferedReader br =null;

     public String [] stringArray;
     public int counLine = 0;
     public int arrayLength ;
      public String s="";
    public String stringLine="";
     public String filename ="";
     public String wordname ="";

public WordData(){

    try{
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the filename: ");
        filename = scan.nextLine();
        Scanner scan2 = new Scanner(System.in);
        System.out.println("Please enter a word: ");
        wordname = scan.nextLine();
        fr = new FileReader(filename);
        br = new BufferedReader(fr);
        while((s = br.readLine()) != null){
            stringLine = stringLine + s;
            //System.out.println(s);
            stringLine = stringLine + " ";
            counLine ++;
        }





        stringArray = stringLine.split(" ");
        arrayLength = stringArray.length;
        for (int i = 0; i < arrayLength; i++) {
            int c = 1 ;
            for (int j = i+1; j < arrayLength; j++) {
                if(stringArray[i].equalsIgnoreCase(stringArray[j])){
                    c++;
                    for (int j2 = j; j2 < arrayLength; j2++) {
                        stringArray[j2] = stringArray[j2+1];
                        arrayLength = arrayLength - 1;
                    }

                       if (stringArray[i].equalsIgnoreCase(wordname)){
           System.out.println("The word "+wordname+" is present "+c+" times in the specified file.");
           }
                }
        }
    }
        System.out.println("Total number of lines: "+counLine);
        fr.close();
        br.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);
    OutputStream out = new FileOutputStream("output.txt");
    System.out.println("Please enter the filename: ");
    String filename = scan.nextLine();
    System.out.println("Please enter a word: ");
    String wordname = scan.nextLine();
    int count = 0;
    try (LineNumberReader r = new LineNumberReader(new FileReader(filename))) {
        String line;
        while ((line = r.readLine()) != null) {
            for (String element : line.split(" ")) {
                if (element.equalsIgnoreCase(wordname)) {
                    count++;
                    System.out.println("Word found at line " + r.getLineNumber());
                }
            }
        }
    }
                        FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append("\n");
            }
            fileReader.close();
    System.out.println("The word " + stringBuffer.toString() + " appears " + count + " times.");
    int i;
    List<String> ls = new ArrayList<String>();
    for (i = 1; i <= 1000; i++) {
        String str = null;
        str = +i + ":-  The word "+wordname+" was found " + count +"  times";
        ls.add(str);
    }
    String listString = "";
    for (String s : ls) {
        listString += s + "\n";
    }
    FileWriter writer = null;
    try {
        writer = new FileWriter("final.txt");
        writer.write(listString);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

The code below does something like you want I think.

it does the following:

  • read the contents from the input.txt file
  • Remove punctuation marks from the text
  • make it one string of words by removing line breaks
  • Split the text up in words by using space as delimiter
  • The lambda maps all the words to lowercase then removes whitespace and all empty entries then it...
  • loops over all words and computes there word count in het HashMap
  • then we sort the Map based on the count value in reverse order to get the highest counted words first
  • then write them to a StringBuilder to format it like this "word : count\\n" and then write it to a text file

     final String content = new String(Files.readAllBytes(Paths.get("<PATH TO YOUR PLACE>/input.txt"))); final List<String> words = Arrays.asList(content.replaceAll("[\\\\p{InCombiningDiacriticalMarks}]", "").replace("\\n", " ").split(" ")); final Map<String, Integer> wordlist = new HashMap<>(); words.stream() .map(String::toLowerCase) .map(String::trim) .filter(s -> !s.isEmpty()) .forEach(s -> { wordlist.computeIfPresent(s, (s1, integer) -> ++integer); wordlist.putIfAbsent(s, 1); }); final StringBuilder sb = new StringBuilder(); wordlist.entrySet() .stream() .sorted(Map.Entry.comparingByValue(Collections.reverseOrder())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )).forEach((s, integer) -> sb.append(s).append(" : ").append(integer).append("\\n")); Files.write(Paths.get("<PATH TO YOUR PLACE>/output.txt"), sb.toString().getBytes()); 

Hope it helps :-)

Note: the <PATH TO YOUR PLACE> needs to be replaced by the fully qualified path to your text file with words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM