for(int i=0; i<pricesArray.length-1;i++)
{
double value = Double.valueOf(pricesArray[i]).doubleValue();
sum = value + sum;
}
Your for loop is missing one of the elements.
You can change it to this:
for(int i=0; i<strong><</strong>pricesArray.length;i++)
Note: you don't have to worry about indexing out of bounds because it never hits 5, it stays less than five
Or you could do this:
for(int i=0; i<strong><=</strong>pricesArray.length-1;i++)
Note: the change is the = sign.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
I think what your wanting is an arraylist..
Try this out and see what you think...I'm pretty sure it works but haven't tested it:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Average extends JApplet implements ActionListener
{
Container con = getContentPane();
JLabel displayAverage = new JLabel(); // displays the calculated average price of the numbers entered
JButton enterPrice = new JButton("Enter Price"); // enters the prices entered by the user into memory
JButton calculateAverage = new JButton("Average"); // calculates the average of the prices entered
JTextField userPrice = new JTextField(10); // allows user to enter prices
ArrayList pricesArray = new ArrayList();
int count = 0;
public void init()
{
con.add(calculateAverage);
con.add(enterPrice);
con.add(userPrice);
con.add(displayAverage);
con.setLayout(new FlowLayout(FlowLayout.CENTER));
userPrice.requestFocus();
enterPrice.addActionListener(this);
calculateAverage.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == enterPrice)
{
pricesArray.add(userPrice.getText());
userPrice.setText("");
count++;
}
if(source == calculateAverage)
{
double sum = 0;
double average;
for(int i=0; i<pricesArray.size();i++)
{
double value = Double.parseDouble(((String)pricesArray.get(i)));
sum = value + sum;
}
average=sum/count;
String str = Double.toString(average);
displayAverage.setText("The Average Price Is: $" + str);
count=0;
}
}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Xlint is one of java 1.5's new features. I added an array list so that's why it's complaining. You can recompile like this:
javac Average.java -Xlint
then it will list a bunch of warnings and such. I did test the code, and it worked for me. If it doesn't work for you, check the java console and let me know what it's saying.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Those really aren't erros, those are warnings. You don't even have to worry about them.
What it is, the new version is wanting you to use the new features as opposed to old. Basicly in this case it's wanting you to use generics, but I wouldn't worry about it. You'll see when you get deeper into application that you might get 10-13 warnings, but you can just ignore those. If you wanted to fix that you would have visit type safety, but I wouldn't worry about 1.5 features unless you're well versed in normal 1.4 java.
Anyways, glad you got it fixed, and glad I could help.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20