Results 1 to 6 of 6
Thread: Struggling with this code
- 07-14-2011, 05:16 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
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!
- 07-14-2011, 05:45 PM #2
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 05:47 PM.
- 07-14-2011, 05:49 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
it does output the general applet but it doesnt out put the amount of entries of each word put in the Jtextfield
- 07-14-2011, 05:58 PM #4
There is no JTextField in you program.it doesnt out put the amount of entries of each word put in the Jtextfield
- 07-14-2011, 06:03 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
sorry i meant the JTextArea.
- 07-14-2011, 06:06 PM #6
Similar Threads
-
Struggling with enum!
By XmisterIS in forum New To JavaReplies: 4Last Post: 09-03-2010, 12:23 PM -
Struggling with AlphaComposite .. please help me
By jeshmal4u in forum Java 2DReplies: 3Last Post: 03-08-2010, 05:11 AM -
Struggling with OnKeyPressed Event in java
By Camzie in forum NetBeansReplies: 2Last Post: 12-01-2009, 07:58 AM -
Struggling with java .ini files
By Camzie in forum NetBeansReplies: 6Last Post: 11-24-2009, 03:13 PM -
struggling with creating a GUI
By tuwannu in forum Advanced JavaReplies: 2Last Post: 12-01-2008, 02:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks