Monday, November 9, 2015

Word counter

Oddly enough, the name is player but this doesn't play anything. I named it a Player for no reason.

This is a GUI which counts how many times a word is repeated in a .txt file.

You need to previously prepare a .txt file to search.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JScrollPane;

public class Player extends JFrame{

  /**
  *
  */
  private static final long serialVersionUID = 1L;
  private JTextField textField;
  private JTextArea textArea;
  private final Action action_1 = new Filechoose();
  JFrame frame;
  private final Action action = new Go();
  private String pathA = "";
  private JTextField textField_1;
  public static void main(String[] args){
 

    Player frame = new Player();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(10, 10, 500, 300);
    frame.setTitle("Word counter");
    frame.setVisible(true);
  }

  Player(){

    JPanel textPanel = new JPanel();
    pathA = "";
 
    getContentPane().add(textPanel, BorderLayout.CENTER);
    textPanel.setLayout(null);
 
    JButton Go = new JButton("Go");
    Go.setAction(action);
    Go.setBounds(45, 231, 91, 21);
    textPanel.add(Go);
 
    textArea = new JTextArea();
    textArea.setBounds(35, 75, 365, 139);

 
    textField = new JTextField();
    textField.setBounds(35, 10, 303, 19);
    textPanel.add(textField);
    textField.setColumns(10);
 
    JButton choose = new JButton("C");
    choose.setAction(action_1);
    choose.setBounds(350, 11, 50, 21);
    textPanel.add(choose);
 
    textField_1 = new JTextField();
    textField_1.setBounds(35, 46, 96, 19);
    textPanel.add(textField_1);
    textField_1.setColumns(10);
 
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setBounds(35,75,365,139);
    textPanel.add(scrollPane);
  }

 private class Filechoose extends AbstractAction {
  private static final long serialVersionUID = 1L;
  public Filechoose() {
            putValue(NAME, "C"); //Name
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e) {
            JFileChooser filechooser = new JFileChooser(); // Class for choosing a file

            int selected = filechooser.showOpenDialog(frame); //dialogue for getting the file path
            if (selected == JFileChooser.APPROVE_OPTION){
                File file = filechooser.getSelectedFile();
                textField.setText(file.getPath());
                textArea.append("Chosen: " + file.getName() + "\n");
                pathA = file.getPath();
             }
        }
    }
 
 private class Go extends AbstractAction {
  private static final long serialVersionUID = 1L;
  public Go() {
   putValue(NAME, "Go"); // Name
   putValue(SHORT_DESCRIPTION, "Some short description");
  }
 
  public void actionPerformed(ActionEvent e) {
   try {
    textArea.setText("");
    File file1 = new File(pathA);
       Scanner scan;
    scan = new Scanner(file1);
    scan.useDelimiter("\\r\\n");
    int count = 0;
    int count2 = 0;
    String strF = "";
    textArea.append("Your word is '" + textField_1.getText() + "'.\n");
   
    while(scan.hasNext()) {
     count2++;
        String str2 = scan.next();
        String str3 = textField_1.getText();
        Pattern pattern1 = Pattern.compile(str3);
            Matcher m = pattern1.matcher(str2);
         
             while(m.find()){
                 count++;
                 String strF2 = "";
              strF2 = "Line "+ count2 + "\n";
              strF = strF + strF2;
             }
    }
    textArea.append("This word is " + count + " times repeated in this file.\n");
    textArea.append("Found line: \n" + strF);
    scan.close();
   } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }

  }
 }
}