7 Replies - 17728 Views - Last Post: 01 June 2008 - 12:01 PM Rate Topic: -----

#1 C:/Syntax   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Finding average Word length, words in a ArrayList

Posted 30 May 2008 - 10:51 AM

I am trying to get the average word length. Each word is stored in the ArrayList. Each time I enter a words, it find out the first ABC words, and last ABC Word. How can i get the average word length.
This is all in addWord().
I made the for loop. I can't seem to get the the sum of the words, and find the average.

The part that I cannot get is commented out.

My Code:
import java.util.*;//array list
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * Class WordTest - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
public class WordTest extends JApplet
  implements ActionListener
{
  private JLabel wordLabel, firstLabel, lastLabel, lengthLabel, randomLabel;
  private JTextField wordField, firstField, lastField, lengthField, randomField;
  private JButton randomButton;
  private ArrayList <String> words;//ArrayList of Strings
  private String word;
  private static Random random=new Random();
  
  public void init()
  {
	JPanel topPanel =  new JPanel();
	JPanel bottomPanel =  new JPanel();
	
	words = new ArrayList<String>();
	
	wordLabel = new JLabel("Enter Word");
	wordField = new JTextField("",10);
	randomButton = new JButton("Random Word?");
	
	firstLabel = new JLabel("First Word");
	firstField = new JTextField("",10);
	lastLabel = new JLabel("Last Word");
	lastField = new JTextField("",10);
	lengthLabel = new JLabel("Length");
	lengthField = new JTextField("",10);
	randomLabel = new JLabel("Random Word");
	randomField = new JTextField("",10);
	
	topPanel.setLayout(new FlowLayout());
	topPanel.add(wordLabel);
	topPanel.add(wordField);
	topPanel.add(randomButton);
	
	bottomPanel.setLayout(new FlowLayout());
	bottomPanel.add(firstLabel);
	bottomPanel.add(firstField);
	bottomPanel.add(lastLabel);
	bottomPanel.add(lastField);
	bottomPanel.add(lengthLabel);
	bottomPanel.add(lengthField);
	bottomPanel.add(randomLabel);
	bottomPanel.add(randomField);
	
	Container window = getContentPane();
	window.setLayout(new BorderLayout());
	window.add(topPanel, BorderLayout.NORTH);
	window.add(bottomPanel, BorderLayout.SOUTH);
	wordField.addActionListener(this);
	randomButton.addActionListener(this);
  }
  
  public void actionPerformed( ActionEvent ae)
  {
	if(ae.getSource()==wordField)
	{
	  addWord();
	}
	else if(ae.getSource()==randomButton)
	{
		randomize();
	}
  }
  
  public void addWord()
  {
	int num, sum;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
//	 for(int i=0; i<words.size();i++)
//	 {
//	   num = words.length();
//	   sum = sum+num;
//	 }

	wordField.setText("");
  }
  
  public void randomize()
  {
	  int randomNum;
	  randomNum= random.nextInt(words.size());
	  randomField.setText(words.get(randomNum));;
	  
	  
  }
  
  private static int stringToInt(String stringObject)
  {
	return Integer.parseInt(stringObject.trim());
  }
  
  
}


addWord() ( I put this here for clairity!)
  public void addWord()
  {
	int num, sum;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
//	 for(int i=0; i<words.size();i++)
//	 {
//	   num = words.length();
//	   sum = sum+num;
//	 }


Is This A Good Question/Topic? 0
  • +

Replies To: Finding average Word length, words in a ArrayList

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,222
  • Joined: 07-April 08

Re: Finding average Word length, words in a ArrayList

Posted 30 May 2008 - 10:54 AM

View PostC:/Syntax, on 30 May, 2008 - 10:51 AM, said:

I am trying to get the average word length. Each word is stored in the ArrayList. Each time I enter a words, it find out the first ABC words, and last ABC Word. How can i get the average word length.
This is all in addWord().
I made the for loop. I can't seem to get the the sum of the words, and find the average.

The part that I cannot get is commented out.

My Code:
import java.util.*;//array list
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * Class WordTest - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
public class WordTest extends JApplet
  implements ActionListener
{
  private JLabel wordLabel, firstLabel, lastLabel, lengthLabel, randomLabel;
  private JTextField wordField, firstField, lastField, lengthField, randomField;
  private JButton randomButton;
  private ArrayList <String> words;//ArrayList of Strings
  private String word;
  private static Random random=new Random();
  
  public void init()
  {
	JPanel topPanel =  new JPanel();
	JPanel bottomPanel =  new JPanel();
	
	words = new ArrayList<String>();
	
	wordLabel = new JLabel("Enter Word");
	wordField = new JTextField("",10);
	randomButton = new JButton("Random Word?");
	
	firstLabel = new JLabel("First Word");
	firstField = new JTextField("",10);
	lastLabel = new JLabel("Last Word");
	lastField = new JTextField("",10);
	lengthLabel = new JLabel("Length");
	lengthField = new JTextField("",10);
	randomLabel = new JLabel("Random Word");
	randomField = new JTextField("",10);
	
	topPanel.setLayout(new FlowLayout());
	topPanel.add(wordLabel);
	topPanel.add(wordField);
	topPanel.add(randomButton);
	
	bottomPanel.setLayout(new FlowLayout());
	bottomPanel.add(firstLabel);
	bottomPanel.add(firstField);
	bottomPanel.add(lastLabel);
	bottomPanel.add(lastField);
	bottomPanel.add(lengthLabel);
	bottomPanel.add(lengthField);
	bottomPanel.add(randomLabel);
	bottomPanel.add(randomField);
	
	Container window = getContentPane();
	window.setLayout(new BorderLayout());
	window.add(topPanel, BorderLayout.NORTH);
	window.add(bottomPanel, BorderLayout.SOUTH);
	wordField.addActionListener(this);
	randomButton.addActionListener(this);
  }
  
  public void actionPerformed( ActionEvent ae)
  {
	if(ae.getSource()==wordField)
	{
	  addWord();
	}
	else if(ae.getSource()==randomButton)
	{
		randomize();
	}
  }
  
  public void addWord()
  {
	int num, sum;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
//	 for(int i=0; i<words.size();i++)
//	 {
//	   num = words.length();
//	   sum = sum+num;
//	 }

	wordField.setText("");
  }
  
  public void randomize()
  {
	  int randomNum;
	  randomNum= random.nextInt(words.size());
	  randomField.setText(words.get(randomNum));;
	  
	  
  }
  
  private static int stringToInt(String stringObject)
  {
	return Integer.parseInt(stringObject.trim());
  }
  
  
}


addWord() ( I put this here for clairity!)
  public void addWord()
  {
	int num, sum;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
//	 for(int i=0; i<words.size();i++)
//	 {
//	   num = words.length();
//	   sum = sum+num;
//	 }



i'm not 100% sure on this but cant you use words[i].length();?
Was This Post Helpful? 0
  • +
  • -

#3 C:/Syntax   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Re: Finding average Word length, words in a ArrayList

Posted 30 May 2008 - 06:40 PM

This is case is a done deal. I was being stupid the whole day, in school today, Computer Science class really. This was so simple a cave man could do it.

Thanks for all who looked at this, and reviewed it.

Here is the for loop fixed with the average included:
	for(int i=0; i<words.size();i++)
	{
	  num = word.length();
	  sum = sum+num;
	}
	 avg= (double)sum/words.size();
	 lengthField.setText(""+avg);


Quote

Above, on my first post, I did words.length().
Notice that words, is an ArrayList of String!
I had to do word(plural), which is the String that I am inputting and adding to the ArrayList(String>


method addWord:
  public void addWord()
  {
	int num, sum=0;
	double avg;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
	for(int i=0; i<words.size();i++)
	{
	  num = word.length();
	  sum = sum+num;
	}
	 avg= (double)sum/words.size();
	 lengthField.setText(""+avg);

	wordField.setText("");
  }


Program in whole:
import java.util.*;//array list
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
* Class WordTest - write a description of the class here
* 
* @author (your name) 
* @version (a version number)
*/
public class WordTest extends JApplet
  implements ActionListener
{
  private JLabel wordLabel, firstLabel, lastLabel, lengthLabel, randomLabel;
  private JTextField wordField, firstField, lastField, lengthField, randomField;
  private JButton randomButton;
  private ArrayList <String> words;//ArrayList of Strings
  private String word;
  private static Random random=new Random();
  
  public void init()
  {
	JPanel topPanel =  new JPanel();
	JPanel bottomPanel =  new JPanel();
	
	words = new ArrayList<String>();
	
	wordLabel = new JLabel("Enter Word");
	wordField = new JTextField("",10);
	randomButton = new JButton("Random Word?");
	
	firstLabel = new JLabel("First Word");
	firstField = new JTextField("",10);
	lastLabel = new JLabel("Last Word");
	lastField = new JTextField("",10);
	lengthLabel = new JLabel("Average Word Length");
	lengthField = new JTextField("",10);
	randomLabel = new JLabel("Random Word");
	randomField = new JTextField("",10);
	
	topPanel.setLayout(new FlowLayout());
	topPanel.add(wordLabel);
	topPanel.add(wordField);
	topPanel.add(randomButton);
	
	bottomPanel.setLayout(new FlowLayout());
	bottomPanel.add(firstLabel);
	bottomPanel.add(firstField);
	bottomPanel.add(lastLabel);
	bottomPanel.add(lastField);
	bottomPanel.add(lengthLabel);
	bottomPanel.add(lengthField);
	bottomPanel.add(randomLabel);
	bottomPanel.add(randomField);
	
	Container window = getContentPane();
	window.setLayout(new BorderLayout());
	window.add(topPanel, BorderLayout.NORTH);
	window.add(bottomPanel, BorderLayout.SOUTH);
	wordField.addActionListener(this);
	randomButton.addActionListener(this);
  }
  
  public void actionPerformed( ActionEvent ae)
  {
	if(ae.getSource()==wordField)
	{
	  addWord();
	}
	else if(ae.getSource()==randomButton)
	{
		randomize();
	}
  }
  
  public void addWord()
  {
	int num, sum=0;
	double avg;
	
	word = wordField.getText();
	words.add(word.toUpperCase());
	Collections.sort(words);
	firstField.setText(words.get(0));
	lastField.setText(words.get(words.size()-1));
	
	for(int i=0; i<words.size();i++)
	{
	  num = word.length();
	  sum = sum+num;
	}
	 avg= (double)sum/words.size();
	 lengthField.setText(""+avg);

	wordField.setText("");
  }
  
  public void randomize()
  {
	  int randomNum;
	  randomNum= random.nextInt(words.size());
	  randomField.setText(words.get(randomNum));;
	  
	  
  }
  
  private static int stringToInt(String stringObject)
  {
	return Integer.parseInt(stringObject.trim());
  }
  
  
}

This post has been edited by C:/Syntax: 30 May 2008 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Finding average Word length, words in a ArrayList

Posted 30 May 2008 - 08:54 PM

   for(int i=0; i<words.size();i++)	 {
	   num = words.get(i).length();
	   sum = sum+num;
   }


Was This Post Helpful? 0
  • +
  • -

#5 C:/Syntax   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Re: Finding average Word length, words in a ArrayList

Posted 31 May 2008 - 10:05 AM

View Postpbl, on 30 May, 2008 - 11:54 PM, said:

   for(int i=0; i<words.size();i++)	 {
	   num = words.get(i).length();
	   sum = sum+num;
   }




Does this compile, i don't think it does.
Was This Post Helpful? 0
  • +
  • -

#6 mensahero   User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 680
  • Joined: 26-May 08

Re: Finding average Word length, words in a ArrayList

Posted 31 May 2008 - 10:34 AM

View PostC:/Syntax, on 31 May, 2008 - 10:05 AM, said:

View Postpbl, on 30 May, 2008 - 11:54 PM, said:

   for(int i=0; i<words.size();i++)	 {
	   num = words.get(i).length();
	   sum = sum+num;
   }




Does this compile, i don't think it does.



well.. mostly would probably say that the right "words" is the right thing.. because in your code it is actually used often.. they probably think you had a typo..

you created to almost exact variable..

words = arraylist
word = string

thats not a good practice.. just my opinion..

This post has been edited by mensahero: 31 May 2008 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Finding average Word length, words in a ArrayList

Posted 31 May 2008 - 01:09 PM

View PostC:/Syntax, on 31 May, 2008 - 10:05 AM, said:

View Postpbl, on 30 May, 2008 - 11:54 PM, said:

   for(int i=0; i<words.size();i++)	 {
	   num = words.get(i).length();
	   sum = sum+num;
   }




Does this compile, i don't think it does.


Should do
words (with a s) is an ArrayList of String where you put the String word (without s)
In the first version you where adding the length of last used word (without s) for the size of the ArrayList words.
The loop should extract every word from words and that add the length of that word

P.S. avoid using so confusing variable name:
ArrayList<String> wordList;
String wordStr;
would be better choices
Was This Post Helpful? 0
  • +
  • -

#8 C:/Syntax   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Re: Finding average Word length, words in a ArrayList

Posted 01 June 2008 - 12:01 PM

Quote

pbl:
P.S. avoid using so confusing variable name:
ArrayList<String> wordList;
String wordStr;
would be better choices


Thanks for this advice, I never thought about naming my variables like that for ArrayList's.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1