Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Word length frequency help for applet

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Word length frequency help for applet

    Hi all, I have been set an assignment in university. I have to create an applet counts the word length frequency and have looked all over to see if anyone else has done this. So far I have been able to work out how to count the number of words and that's all.
    It says in my assignment criteria:

    "In its simplest form, frequency analysis
    calculates the number of words of length 1, 2, 3, ..., n, where n is the length of
    the longest word. For example, analysing the sentence “I am a man” would
    produce the output “2, 1, 1”." In pretty sure the outcome is 2,1,1 because it has 2 words with 1 letter, 1 word with 2 letters and 1 with 3 letters.

    Here is my code so far:

    import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
     
     
        public class Sykes_J_resitAss extends JApplet 
                                          implements ActionListener {
     
           JTextArea textInput;     // For the user's input text.
     
     
           JLabel wordCountLabel;   // For displaying the number of words.
     
     
     
           public void init() {
     
              setBackground(Color.darkGray);
              getContentPane().setBackground(Color.darkGray);
     
              /* Create the text input area and make sure it has a
                 white background. */
     
              textInput = new JTextArea();
              textInput.setBackground(Color.white);
     
              /* Create a panel to hold the button and three display
                 labels.  These will be laid out in a GridLayout with
                 4 rows and 1 column. */
     
              JPanel south = new JPanel();
              south.setBackground(Color.darkGray);
              south.setLayout( new GridLayout(4,1,2,2) );
     
              /* Create the button, set the applet to listen for
                 clicks on the button, and add it to the panel. */
     
              JButton countButton = new JButton("Process the Text");
              countButton.addActionListener(this);
              south.add(countButton);
     
              /* Create each of the labels, set their colors, and
                 add them to the panel. */
     
     
              wordCountLabel = new JLabel("  Number of words:");
              wordCountLabel.setBackground(Color.white);
              wordCountLabel.setForeground(Color.blue);
              wordCountLabel.setOpaque(true);
              south.add(wordCountLabel);
     
     
              /* Use a BorderLayout on the applet.  Although a BorderLayout
                 is the default, I want one with a vertical gap of two
                 pixels, to let the dark gray background color show through. */
     
              getContentPane().setLayout( new BorderLayout(2,2) );
     
              /* The text area is put into a JScrollPane to provide
                 scroll bars for the TextArea, and the scroll pane is put in
                 the Center position.  The panel that holds the button and
                 labels is in the South position.  Note that the text area
                 will be sized to fill the space that is left after the
                 panel is assigned its preferred height. */
     
              JScrollPane scroller = new JScrollPane( textInput );
              getContentPane().add(scroller, BorderLayout.CENTER);
              getContentPane().add(south, BorderLayout.SOUTH);
     
           } // end init();
     
     
           public Insets getInsets() {
                 // Leave a 2-pixel border around the edges of the applet.
              return new Insets(2,2,2,2);
           }
     
     
           public void actionPerformed(ActionEvent evt) {
                 // Respond when the user clicks on the button by getting
                 // the text from the text input area, counting the number
                 // of chars, words, and lines that it contains, and
                 // setting the labels to display the data.
     
               String text;  // The user's input from the text area.
     
               int charCt, wordCt, lineCt;  // Char, word, and line counts.
     
               text = textInput.getText();
     
               charCt = text.length();  // The number of characters in the
                                        //    text is just its length.
     
               /* Compute the wordCt by counting the number of characters
                  in the text that lie at the beginning of a word.  The
                  beginning of a word is a letter such that the preceding
                  character is not a letter.  This is complicated by two
                  things:  If the letter is the first character in the
                  text, then it is the beginning of a word.  If the letter
                  is preceded by an apostrophe, and the apostrophe is
                  preceded by a letter, than its not the first character
                  in a word.
               */
     
               wordCt = 0;
               for (int i = 0; i < charCt; i++) {
                  boolean startOfWord;  // Is character i the start of a word?
                  if ( Character.isLetter(text.charAt(i)) == false )
                     startOfWord = false;  // No.  It's not a letter.
                  else if (i == 0)
                     startOfWord = true;   // Yes.  It's a letter at start of text.
                  else if ( Character.isLetter(text.charAt(i-1)) )
                     startOfWord = false;  // No.  It's a letter preceded by a letter.
                  else if ( text.charAt(i-1) == '\'' && i > 1 
                                       && Character.isLetter(text.charAt(i-2)) )
                     startOfWord = false;  // No.  It's a continuation of a word
                                           //      after an apostrophe.
                  else
                     startOfWord = true;   // Yes.  It's a letter preceded by
                                           //       a non-letter.
                  if (startOfWord)
                     wordCt++;
               }
     
               /* The number of lines is just one plus the number of times the
                  end of line character, '\n', occurs in the text. */
     
               lineCt = 1;
               for (int i = 0; i < charCt; i++) {
                  if (text.charAt(i) == '\n')
                     lineCt++;
               }
     
               /* Set the labels to display the data. */
     
     
               wordCountLabel.setText("  Number of Words:  " + wordCt);
     
     
           }  // end actionPerformed()
     
     
        } // end class TextCounterApplet

    Any help with this would be greatly appreciated, my deadline is tomorrow and I haven't got a clue! Thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Word length frequency help for applet

    Quote Originally Posted by jake6047 View Post
    Any help with this would be greatly appreciated
    Help with what? We don't read minds. If you get errors then copy and paste the full and exact error message and indicate which line it happens on. If it has incorrect behaviour then explain what it does do and what it should do instead. Most importantly ask a specific question. "It doesn't work" provides zero information and is not specific.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Word length frequency help for applet

    The code doesn't get errors, I need to get the frequency for the length of words like if I entered " Have a nice day" it would output '2,1,1' the reason for this is because 2 words have the same length and 2 don't. My code lets me know how many words there are and I can't figure out how to do this frequency thing. Sorry for not being very clear.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Lightbulb Re: Word length frequency help for applet

    Quote Originally Posted by Junky View Post
    If it has incorrect behaviour then explain what it does do and what it should do instead.
    Quote Originally Posted by jake6047 View Post
    For example, analysing the sentence “I am a man” would
    produce the output “2, 1, 1”." In pretty sure the outcome is 2,1,1 because it has 2 words with 1 letter, 1 word with 2 letters and 1 with 3 letters.
    Dear Junky
    I think you have to read posts more carefully before abuse at smb. hurl

    text = textInput.getText();
     
               String array[]=text.split(" ");
               int counter=0;
               for(int i=0;i<array.length;i++)
                   if(counter<array[i].length())
                       counter=array[i].length();
               int intArray[]=new int[counter];
               for(int i=0;i<intArray.length;i++){
                   intArray[i]=0;
               }
               for(int i=0;i<array.length;i++){
                   intArray[array[i].length()-1]++;
               }
               String a="";
               for(int i=0;i<intArray.length;i++){
                   if(intArray[i]>0)
                   {
                    a+=String.valueOf(intArray[i]);
                    a+=", ";
                   }
               }
               wordCountLabel.setText(a);

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Word length frequency help for applet

    Thank you very much Serdar, i've put it into my code and it works perfectly. I really appreciate your help!

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Word length frequency help for applet

    Quote Originally Posted by serdar View Post
    Dear Junky I think you have to read posts more carefully before abuse at smb. hurl
    Actually, I believe Junky was on the spot.
    The OP's desires were clear, but didn't state what issues he was having, giving the impression that he just wants us to do it for him.

    We're here to help and not do people's homework for them. The OP should show goodwill by going off, and attempting it, before reporting back with any errors or concepts he doesn't understand in greater detail.

    Edit: The problem with spoon-feeding. Read it, live it, ??eat it??
    Last edited by newbie; August 26th, 2011 at 07:49 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Llist of words and frequency of each word
    By jkkj in forum Collections and Generics
    Replies: 7
    Last Post: August 16th, 2011, 09:59 AM
  2. max length of an array?
    By qsbladey in forum Collections and Generics
    Replies: 4
    Last Post: April 3rd, 2011, 11:17 AM
  3. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  4. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 06:24 AM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 09:23 AM

Tags for this Thread