简体   繁体   中英

Counting number of times words show up in txt file Java

This program is suppsoed to count the number of times each word shows up in a .txt file I'm on the right track because this program shows every word in the file in a text area but I need it to not repeat words and increment a counter instead. Am I on the right track under the for each word:words statement? I know that I just need some more logic in this statement but am not sure how to implement it. I should probably add that I'm using orderedlinkedlists to put all the words in alphabetical order in the text area so they or in order next to eachother.

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;


public class Oct29 extends JPanel
{
  private OrderedList<String> words;
  private String filename;
  private int width = 800;
  private int height = 600;
  private TextArea textarea;

  public Oct29()
  {
    Scanner scan;
    textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
    textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
    textarea.setPreferredSize(new Dimension(width,height));
    setPreferredSize(new Dimension(width,height));
    add(textarea);
    JFileChooser chooser = new JFileChooser("../Text");
    int returnvalue = chooser.showOpenDialog(null);

    if(returnvalue == JFileChooser.APPROVE_OPTION)
    {
      try
      {
        File file = chooser.getSelectedFile();
        filename = file.getName();
        System.err.println(filename);
        scan = new Scanner(file);
      }
      catch (IOException e)
      {
        System.err.println("IO EXCEPTION");
        return;
      }       
    }
    else
    {
      return;
    }
    words = new OrderedLinkedList<String>();
    while(scan.hasNext())
    {
      String token = scan.next().toLowerCase();
      token = token.replace(",","").replace(".","");
      words.add(token);
    }
    scan.close();
    textarea.append("    "+filename+" has wordcount: "+words.size()+
      "\n-------------------------\n\n");

     for(String word : words)
     {
       for( int i=0;i<words.size(); i++)
       {
       int x =0;       // This does not work but I'm trying to find if I'm on the right track
       if(x < words.size())  
        textarea.append(word+"\n"); // THIS BY ITESELF WITHOUT THE INNER FOR LOOP WORKS FINE FOR DISPLAYING THE WORDS IN A TEXT AREA.
       x++;
     }
     }
  }

  public static void main(String[] arg)
  {
    JFrame frame = new JFrame("Oct 29");
    frame.getContentPane().add(new Oct29());
    frame.pack();
    frame.setVisible(true);
  }
}

我建议尝试另一种方法: 一个单词在TXT文件中出现多少次这也可能有用: 计算字符串在文件中出现的次数

You can use a Map for your problem. It can map your keys (in your case these are the words) to values (integer values reflecting the occurrence of those words). You can initialize your map as follows:

        Map<String, Integer> wordCount=new HashMap<String, Integer>();

Inside the loop you have to check that whether the map already contains that key (the word). If so, then you have to increment the value of the integer associated with it. If it is not added, just add that pair to the map with the value of the integer set to 1.

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