Results 1 to 6 of 6
  1. #1
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default Struggling with this code

    Hi all right ok what I want to do is for the applet to count the number of occurences for each word entered in the JText area and display them eg: if the user enters "hi hi hi hi b " it would come out as :
    "hi: 3
    b: 1"

    this is what Ive got so far:


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Map;
    import java.util.TreeMap;

    public class Analyzer extends JApplet implements ActionListener
    {
    private int charCount = 0; // Setting of Buttons integers labels and text field
    private int wordCount = 0;
    private int lineCount = 0;
    private int wordFreq;




    private JButton analyzeButton = new JButton ( "Analyze" );
    private JButton resetButton = new JButton ( "Reset" );

    private JTextArea textEntry = new JTextArea ("Enter Text", 3,15);

    private JLabel character = new JLabel ("Number of Characters: " + charCount + ",");
    private JLabel lines = new JLabel ("Number of Lines: "+ lineCount + ",");
    private JLabel words = new JLabel ("Frequency of Words: " + wordCount + "," );


    public void init (){
    Container pane = getContentPane(); //Layout
    pane.setLayout (new FlowLayout ());

    pane.add( textEntry );
    pane.add (analyzeButton);
    pane.add (resetButton);
    pane.add (character);
    pane.add (words);
    pane.add (lines);

    analyzeButton.addActionListener(this); // Action Listeners
    resetButton.addActionListener (this);
    }




    public void actionPerformed (ActionEvent e){
    if(e.getSource() == analyzeButton){ // Analyse Button

    int wordFreq= ( String text = textEntry.getText();

    TreeMap<String, Integer> countOfWordsMap = new TreeMap<String, Integer>();

    String[] words = text.split("[()!;: \n\t\r.,?{}]");

    for (String word : words)
    {
    if (word.length() >= 1)
    {
    String keyWord = word.toLowerCase();
    if (countOfWordsMap.get(keyWord) == null)
    {
    countOfWordsMap.put(keyWord, 1);
    }
    else
    {
    int wordCount = countOfWordsMap.get(keyWord);
    wordCount++;
    countOfWordsMap.put(keyWord, wordCount);
    }
    }

    }

    for (Map.Entry<String, Integer> entry: countOfWordsMap.entrySet())
    {
    System.out.println(entry.getValue()+" : "+entry.getKey());
    }
    )
    charCount = ( textEntry.getText().length());

    character.setText("Character Count: " + charCount);

    analyzeButton.setEnabled(false);
    }
    if(e.getSource() == resetButton){ //Resetting of Text
    charCount=0;
    wordCount = 0;
    lineCount = 0;
    textEntry.setText("Enter Text");
    character.setText("Number of Characters" + charCount + "," );
    lines.setText("Number of Lines: "+ lineCount + "," );
    analyzeButton.setEnabled(true);
    }
    }

    }





    please help!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What does your program output now?
    Can you show what it outputs and explain what is wrong?

    Please wrap your posted code in code tags. See the # icon above the input area.

    First problem you have is that your code does not compile. Is it supposed to compile or are you getting compiler errors?
    Last edited by Norm; 07-14-2011 at 06:47 PM.

  3. #3
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    it does output the general applet but it doesnt out put the amount of entries of each word put in the Jtextfield

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    it doesnt out put the amount of entries of each word put in the Jtextfield
    There is no JTextField in you program.

  5. #5
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    sorry i meant the JTextArea.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

Similar Threads

  1. Struggling with enum!
    By XmisterIS in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 01:23 PM
  2. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 06:11 AM
  3. Struggling with OnKeyPressed Event in java
    By Camzie in forum NetBeans
    Replies: 2
    Last Post: 12-01-2009, 08:58 AM
  4. Struggling with java .ini files
    By Camzie in forum NetBeans
    Replies: 6
    Last Post: 11-24-2009, 04:13 PM
  5. struggling with creating a GUI
    By tuwannu in forum Advanced Java
    Replies: 2
    Last Post: 12-01-2008, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •