| | |
user input random amount display average. little problem i have run into
![]() |
•
•
Join Date: Jul 2005
Posts: 9
Reputation:
Solved Threads: 0
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;
}
}
}
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;
}
}
}
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Java Syntax (Toggle Plain Text)
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<pricesArray.length;i++)Or you could do this:
for(int i=0; i<=pricesArray.length-1;i++)•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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:
Try this out and see what you think...I'm pretty sure it works but haven't tested it:
Java Syntax (Toggle Plain Text)
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; } } }
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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.
![]() |
Similar Threads
- Tutorial: User Input: Strings and Numbers [C++] (C++)
Other Threads in the Java Forum
- Previous Thread: test
- Next Thread: Command Prompt Command through Java
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game gis givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






