Analysing word frequencies in applet

i am writing a program in applet to measure the frequencies of word lengths in a given amount of text and display these frequencies during applet. i have written the program and it debugs without error.

however the output says i have 255 words of every length in the text i input, i have been staring at this for hours with no luck, i am aware that the drawstring method lists these outputs horizontally which i will be fixing at a later time.

i am assuming the problem resides in the analyseText method.

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.util.*;


public class StringAnalysis extends Applet implements MouseListener, ActionListener  {
Font arielBold;
Label pr_Label;
TextField pr_txt;
int textFieldSize = 15;
Button pr_input1, pr_input2, pr_input3;
String textToAnalyse = ("Nothing has been entered.");
boolean output = false;


    String testing ="";


    //create array to show respective lengths of words
    int [] lengthsOfWords = new int[textFieldSize];
    //first we must separate strings from spaces and populate array for comparison
    String [] arrayOfWords = new String[textFieldSize];


    public void init() {    

        pr_Label = new Label("Enter the text you wish to analise: ");
        add(pr_Label);
        pr_txt = new TextField(textFieldSize);
        add(pr_txt);
        pr_input1 = new Button("Analyse");
        pr_input2 = new Button("Reset");
        add(pr_input1);
        add(pr_input2);
        pr_input1.addActionListener(this);
        pr_input2.addActionListener(this);

    }

    public void start (){
        setSize(1000, 500);
        arielBold = new Font ("Ariel", Font.BOLD, 20);

    }


    public void actionPerformed(ActionEvent e) {


        if (e.getSource() == pr_input2) {
            showStatus("Resseting....");
            reset();
            }
        else if (e.getSource() == pr_input1){
            showStatus("Analysing...a");
            textToAnalyse =  pr_txt.getText();
            analyseText(textToAnalyse);
        }
    }

    public void paint(Graphics g) {


            String statsToOutput = ("There are:" + "\n" );
            int counter = 1;
            for (int lengths: lengthsOfWords) {
                statsToOutput = statsToOutput +lengthsOfWords[0] + " words of length " + counter + "\n ";
            counter ++;
            }
        if (output = true){
        g.drawString(statsToOutput, 25, 200);
        g.drawString("The text to be analysed is: " + textToAnalyse, 25, 100);
        showStatus("Finished.");
        }
        else if (output = false){
            g.setFont(arielBold);
            g.drawString(textToAnalyse,50, 100);
            showStatus("Finished.");
            }
    }

    public void analyseText(String text){
        ///////////////////////////////////////////////////////////////////////////////////////////////////////





        int position1 = 0, position2 = 0;
        String newWord = "";
        String currentLetter;
        int pos1 = 0, pos2 = 0;
        for ( pos1 = 0; pos1 <= arrayOfWords.length-1; pos1++) {

            //Initializes a string object for each address in array
            for (position1 = 0; position1 <= text.length()-1; position1++){

                //steps through each character in text
                currentLetter = Character.toString(text.charAt(position1));

                if (currentLetter.matches("[A-Z][a-z][0-9]")) {
                    //checks if character is alphanumeric using regular expressions (regex)

                    newWord = newWord + currentLetter;
                    //if passes regex then ads character to word
                }
            }
            if (newWord.length() > 0) {
            pos1 = arrayOfWords.length;
            }
            arrayOfWords[pos1] = newWord;


        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////

        emptyArrayInt(lengthsOfWords);

        //now compare each word with rest of array\
        for ( pos2 = 0; pos2 <= arrayOfWords.length-1; pos2++) {


                position2 = 0;
                for (position2 = 0; position2 <= arrayOfWords.length-1; position2++){


                if (arrayOfWords[pos2].length() == arrayOfWords[position2].length());

                lengthsOfWords[arrayOfWords[pos2].length()] = lengthsOfWords[arrayOfWords[pos2].length()] + 1;

            }

        }



        showStatus("finished analysing.");

        output = true;

        repaint();
    }

    public void emptyArrayInt(int[] array) {
        int position = 0;
        for (position = 0; position <= array.length-1; position ++)
        array[position] = 0;
    }
    public void emptyArrayStr(String[] array) {
        int position = 0;
        for (position = 0; position <= array.length-1; position ++)
        array[position] = "";
    }

    public void reset() {

    pr_txt.setText("");
    textToAnalyse = ("Nothing has been entered.");
    emptyArrayInt(lengthsOfWords);
    emptyArrayStr(arrayOfWords);
    //repaint();
    showStatus("Reset Successful.");
    repaint();

    }


    public void mouseClicked(MouseEvent arg0) {}
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}

i would appreciate any help on this please, (I'm desperate).

Answers


Let's try section of code :

private Map<String, int> freqWords = new HashMap<String, int>();

public void analyzeText(String text) {
   // You can split with another type of delimiter
   String regex = ....
   String[] inputs = text.split(regex); 

   for (String s : inputs) {
       if(freqWords.containtsKey(s)) {
           int frequency = inputs.get(s);
           frequency++;
           inputs.put(s, frequency);
       } else {
           inputs.put(s, 1);
       }
   }
}

Hope that it can help you. The main point here is you should use data structure Map to store the frequency words.


Need Your Help

Reducing a large stream without stack overflowing

scala stream range stack-overflow reduce

1I'm trying to make a limit-less factorial function (just out of curiosity.) This works for large n (tried up to 100000 and it seems to work, although I can't check the output value for correctness

PLSQL WHERE date_time is in a range of dates

sql oracle plsql oracle9i

I have two date strings, in the format MM/DD/YYYY and would like to query an Oracle database for any records between those two dates. How do I do this succinctly and simply?