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;
// }

New Topic/Question
Reply



MultiQuote







|