944,004 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2966
  • Java RSS
Jul 10th, 2005
0

user input random amount display average. little problem i have run into

Expand Post »
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;
}

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cman is offline Offline
9 posts
since Jul 2005
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

Java Syntax (Toggle Plain Text)
  1. for(int i=0; i<pricesArray.length-1;i++)
  2. {
  3. double value = Double.valueOf(pricesArray[i]).doubleValue();
  4. sum = value + sum;
  5. }

Your for loop is missing one of the elements.

You can change it to this:
 for(int i=0; i<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<=pricesArray.length-1;i++)
Note: the change is the = sign.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cman is offline Offline
9 posts
since Jul 2005
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

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:

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6. public class Average extends JApplet implements ActionListener
  7. {
  8. Container con = getContentPane();
  9. JLabel displayAverage = new JLabel(); // displays the calculated average price of the numbers entered
  10. JButton enterPrice = new JButton("Enter Price"); // enters the prices entered by the user into memory
  11. JButton calculateAverage = new JButton("Average"); // calculates the average of the prices entered
  12. JTextField userPrice = new JTextField(10); // allows user to enter prices
  13. ArrayList pricesArray = new ArrayList();
  14. int count = 0;
  15. public void init()
  16. {
  17. con.add(calculateAverage);
  18. con.add(enterPrice);
  19. con.add(userPrice);
  20. con.add(displayAverage);
  21. con.setLayout(new FlowLayout(FlowLayout.CENTER));
  22. userPrice.requestFocus();
  23. enterPrice.addActionListener(this);
  24. calculateAverage.addActionListener(this);
  25. }
  26. public void actionPerformed(ActionEvent e)
  27. {
  28. Object source = e.getSource();
  29. if(source == enterPrice)
  30. {
  31. pricesArray.add(userPrice.getText());
  32.  
  33. userPrice.setText("");
  34. count++;
  35. }
  36. if(source == calculateAverage)
  37. {
  38. double sum = 0;
  39. double average;
  40. for(int i=0; i<pricesArray.size();i++)
  41. {
  42. double value = Double.parseDouble(((String)pricesArray.get(i)));
  43. sum = value + sum;
  44. }
  45. average=sum/count;
  46. String str = Double.toString(average);
  47. displayAverage.setText("The Average Price Is: $" + str);
  48. count=0;
  49. }
  50.  
  51. }
  52. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cman is offline Offline
9 posts
since Jul 2005
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

Reputation Points: 10
Solved Threads: 0
Newbie Poster
cman is offline Offline
9 posts
since Jul 2005
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

Thank you for your help
Much appreciated

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


:mrgreen:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cman is offline Offline
9 posts
since Jul 2005
Jul 11th, 2005
0

Re: user input random amount display average. little problem i have run into

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: test
Next Thread in Java Forum Timeline: Command Prompt Command through Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC