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 2 of 2

Thread: How do i add a method to calculate the mean of the length of each word output

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

    Default How do i add a method to calculate the mean of the length of each word output

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;



    public class Assignment extends Applet implements ActionListener
    {
    TextArea textInput = new TextArea(); // user input
    Button analyzebutton = new Button("Analyze");
    Button resetbutton = new Button("Reset");
    Label lbloutput = new Label ("Please enter text into the textbox!");
    Label lblmean = new Label ("");
    int [] xw = {400,410,415,425,435,440,450,445,435,425,415,405};
    int [] yw = {260,260,280,270,280,260,260,300,300,290,300,300};
    int [] xr = {350,370,380,380,370,380,370,360,360,350};
    int [] yr = {260,260,265,275,280,300,300,280,300,300};
    int [] xi = {360,365,370,370,365,360};
    int [] yi = {265,265,270,275,275,275};
    double average, sum = 0.0;

    public void init()
    {
    // give background a color
    setBackground(Color.white);
    setBackground(Color.black);
    lbloutput.setForeground(Color.white);
    lblmean.setForeground(Color.white);

    //add buttons and labels to applet
    add (textInput);
    add (analyzebutton);
    add (resetbutton);
    add (lbloutput);
    add (lblmean);

    //button to listen for mouse click
    analyzebutton.addActionListener(this);
    resetbutton.addActionListener(this);
    }

    public void paint(Graphics g)
    {
    g.setColor(Color.white);
    g.drawPolygon(xw, yw, 12);
    g.drawPolygon(xr, yr, 10);
    g.drawPolygon(xi, yi, 6);
    }


    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource () == analyzebutton)
    {
    String txtInput = textInput.getText ();
    int [] lengthOfWordArray = new int [1000];
    String [] resultSplit = txtInput.split (" ");
    for (int i=0; i<resultSplit.length; i++)
    {
    int length = resultSplit[i].length();
    lengthOfWordArray [length]++;
    }

    String strlength = "There Are: \r\n";
    for (int j=1; j<lengthOfWordArray.length; j++)
    {
    if (lengthOfWordArray [j] !=0)
    {
    strlength += lengthOfWordArray[j] + " Word(s) Of Length " + j + ", \r\n";
    }
    }

    for (int i = 0; i<resultSplit.length; i++)
    {
    int length = resultSplit[i].length ();
    lengthOfWordArray[length]++;
    }

    lbloutput.setText (strlength);
    lbloutput.invalidate();
    validate();
    }
    else if (e.getSource () == resetbutton)
    {
    textInput.setText(" ");
    lbloutput.setText("Please enter text into the textbox!");
    lbloutput.invalidate();
    lbloutput.validate();

    repaint();
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do i add a method to calculate the mean of the length of each word output

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    (Note: I rarely try to read unformatted code)

    Where are the words output? Can the code save the lengths of those words to be used to compute the value you want?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Scan text and add each word alphabetical listing
    By sydethsam in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2014, 07:30 PM
  2. Java word length frequency applet help!
    By tuks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2014, 09:26 AM
  3. Limit length of characters output in JSP string
    By me10lee83 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 24th, 2013, 09:45 PM
  4. Average length of time for input/output
    By ManInTheMiddle in forum Threads
    Replies: 33
    Last Post: April 4th, 2013, 03:24 PM
  5. Word length frequency help for applet
    By jake6047 in forum Java Applets
    Replies: 5
    Last Post: August 26th, 2011, 07:47 AM