Ok I have done this program where there are 2 buttons, one to enter price and one to calc average, so the user enters an randome amount and click the enter price button then user can keep entering more and then click calc average button to get average, but problem is that for some reason the program only works if the user enters 4 amounts. If the user enters 3 orsomething else it doesnt display anything...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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
    String[] pricesArray = new String[5];
    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 && count<5)
            {
                pricesArray[count] = userPrice.getText();

                userPrice.setText("");
                count++;
            }
            if(source == calculateAverage)
            {   
                double sum = 0;
                double average;
                for(int i=0; i<pricesArray.length-1;i++)
                {
                double value = Double.valueOf(pricesArray[i]).doubleValue();
                sum = value + sum;
                }
                average=sum/count;
                String str = Double.toString(average);
                displayAverage.setText("The Average Price Is: $" + str);
                count=0;
            }

    }
}

Recommended Answers

All 8 Replies

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[B]<[/B]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[B]<=[/B]pricesArray.length-1;i++)

Note: the change is the = sign.

hmm well I made the change to the for loop and ran the program, now i can enter 5 numbers and it finds the average

but if enter anything less nothing happens and i cant enter more than 5

frustrating :o

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

}
}

hmm do u know what this error means

Note: Average.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

when i run it all i see is blank

:eek:

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.

Thank you for your help
Much appreciated

it works just those errors u mentioned which shouldnt be a problem


:mrgreen:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.