469,342 Members | 5,614 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

Count input values?

I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.

What can I use to count the input values?

Example: If I type 1, 2 and 0 the average should be 1.5

Ps: I'm a beginner...
Sep 13 '07 #1
21 9167
r035198x
13,262 8TB
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.

What can I use to count the input values?

Example: If I type 1, 2 and 0 the average should be 1.5

Ps: I'm a beginner...
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Sep 13 '07 #2
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Yes I know how to read the input, but how do I count it ?
Sep 13 '07 #3
Nepomuk
3,112 Expert 2GB
Yes I know how to read the input, but how do I count it ?
How would you count them in real life? Think about that, then you'll find the answer.

PS.: Have you found out yet, if the number is positive or negative? That's the first step of course.

Greetings,
Nepomuk
Sep 13 '07 #4
r035198x
13,262 8TB
Yes I know how to read the input, but how do I count it ?
The while loop
Sep 13 '07 #5
I have no idea. Am I on the right track:

import java.util.Scanner;

public class Skilaverkefni2{
public static void main(String[] args) {

Scanner read = new Scanner(System.in);

System.out.println("text: ");

int number = read.nextInt();

while (number != 0) {

}

System.out.println("Quit");
}
}
Sep 13 '07 #6
r035198x
13,262 8TB
I have no idea. Am I on the right track:

import java.util.Scanner;

public class Skilaverkefni2{
public static void main(String[] args) {

Scanner read = new Scanner(System.in);

System.out.println("text: ");

int number = read.nextInt();

while (number != 0) {

}

System.out.println("Quit");
}
}
1.) Use code tags for posting code
2.) Try it on the compiler and see what it does or doesn't do.
Sep 13 '07 #7
Expand|Select|Wrap|Line Numbers
  1.  
  2. while (number != 0) {
  3. number = read.nextInt();
  4. };
  5. System.out.println("Quit"); 
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
Sep 13 '07 #8
Expand|Select|Wrap|Line Numbers
  1.  
  2. while (number != 0) {
  3. number = read.nextInt();
  4. };
  5. System.out.println("Quit"); 
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
Sep 13 '07 #9
Nepomuk
3,112 Expert 2GB
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
Yes, you are. Now think about how you can calculate the average - how would you do it in real life?

Greetings,
Nepomuk
Sep 13 '07 #10
r035198x
13,262 8TB
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
You need a counter (int) to count the numbers and a double variable to store the total and perhaps another one for the average.
Sep 13 '07 #11
Yes, you are. Now think about how you can calculate the average - how would you do it in real life?

Greetings,
Nepomuk
I would add them together, count the amount of numbers and divide with the amount.

What I learned is that this code reads the next input number, but overwrites it if I decide to continue with a new number I put in.

Expand|Select|Wrap|Line Numbers
  1. while (number != 0) {
  2. number = read.nextInt();
  3. };
  4. System.out.println("Quit"); 
I put 25, 43 and 55 in. If I add System.out.println(number) I get the last number 55.
Sep 13 '07 #12
r035198x
13,262 8TB
I would add them together, count the amount of numbers and divide with the amount.

What I learned is that this code reads the next input number, but overwrites it if I decide to continue with a new number I put in.

Expand|Select|Wrap|Line Numbers
  1. while (number != 0) {
  2. number = read.nextInt();
  3. };
  4. System.out.println("Quit"); 
I put 25, 43 and 55 in. If I add System.out.println(number) I get the last number 55.
Perhaps now is the time to read a tutorial?
Sep 13 '07 #13
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. int number  = read.nextInt();
  3.  
  4. while (number != 0) {
  5.     number = read.nextInt();
  6.     if (number != 0) count++; 
  7.     };
  8.     System.out.println("Quit....");
  9.  
This is one way to count how often input numbers are put in.
Sep 13 '07 #14
Nepomuk
3,112 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. int number  = read.nextInt();
  3.  
  4. while (number != 0) {
  5.     number = read.nextInt();
  6.     if (number != 0) count++; 
  7.     };
  8.     System.out.println("Quit....");
  9.  
This is one way to count how often input numbers are put in.
Good, now you have the last number and the amount of numbers. You just need the sum of all numbers now... You're nearly there!

Greetings,
Nepomuk
Sep 13 '07 #15
What I can't figure out is the unspecified amount of numbers which are put in. For example if it would always be 5 numbers this would be so much easier.
Sep 13 '07 #16
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. int number  = read.nextInt();
  3.  
  4. while (number != 0) {
  5.     number = read.nextInt();
  6.     if (number != 0) count++; 
  7.     };
  8.     System.out.println("Quit....");
  9.  
This is one way to count how often input numbers are put in.
Here's a little programming tip: you can express what you want here a bit more
efficiently; this idiom is used a lot by more experienced and older twits like me:

Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2.  
  3. for (int number; (number= read.nextInt()) != 0; count++) {
  4.    // your valuable code here using 'number'
  5.    // no need to test for 0 here again.
  6. }
  7.  
kind regards,

Jos
Sep 13 '07 #17
Nepomuk
3,112 Expert 2GB
What I can't figure out is the unspecified amount of numbers which are put in. For example if it would always be 5 numbers this would be so much easier.
But it doesn't make any difference in this case! Let's see, you've got this code:
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. int number  = read.nextInt();
  3.  
  4. while (number != 0) {
  5.     number = read.nextInt();
  6.     if (number != 0) count++;
  7.     // do something with "number"
  8. } // You don't need a semicolon here, although it isn't wrong
  9. System.out.println("Quit....");
  10.  
or, as JosAH suggested:
Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2.  
  3. for (int number; (number= read.nextInt()) != 0; count++) {
  4.    // your valuable code here using 'number'
  5.    // no need to test for 0 here again.
  6. }
  7.  
Now you have to know, what the sum of all numbers, that were typed in at some point, is. Say, you have another variable: int sum...

By the way, you do know that variables are, as the name says, variable? So their value can be changed, even when they are read out to do so:
Expand|Select|Wrap|Line Numbers
  1. int a = 3;
  2. a *= 3; // or you can use a = a * 3;
  3.  
Greetings,
Nepomuk
Sep 13 '07 #18
I did a little research. Here's my code. Tell me how you like it:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ListingBls99 {
  4.   public static void main(String[] args) {
  5.  
  6.     String dataString = JOptionPane.showInputDialog(
  7.                                                     "Enter a number:\n
  8. (The program will shut down and give you your resault if you press 0 )");
  9.  
  10. double data = Double.parseDouble(dataString);
  11.  
  12.     double countMinus = 0.0;
  13.     double countPlus = 0.0;
  14.     double countAll = 0.0;
  15.     double sum = 0.0;
  16.  
  17.     while (data != 0.0) {
  18.       sum += data;
  19.       if (data < 0.0) countMinus++;
  20.       if (data > 0.0) countPlus++;
  21.       if (data != 0.0) countAll++;
  22.  
  23.     double average = sum/countAll;
  24.  
  25.       dataString = JOptionPane.showInputDialog(
  26.                                                "Enter a number:\n
  27. (The program will shut down and give you your resault if you press 0 )");
  28.  
  29. data = Double.parseDouble(dataString);
  30.  
  31.       JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers:  " + (average = Math.round(average*100.0)/100.0)));
  32.  
  33.     }
  34.   }
  35. }
  36.  
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.

The code works but it probably isn't well organized nor the best way to this.
Sep 13 '07 #19
r035198x
13,262 8TB
I did a little research. Here's my code. Tell me how you like it:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ListingBls99 {
  4.   public static void main(String[] args) {
  5.  
  6.     String dataString = JOptionPane.showInputDialog(
  7.                                                     "Enter a number:\n
  8. (The program will shut down and give you your resault if you press 0 )");
  9.  
  10. double data = Double.parseDouble(dataString);
  11.  
  12.     double countMinus = 0.0;
  13.     double countPlus = 0.0;
  14.     double countAll = 0.0;
  15.     double sum = 0.0;
  16.  
  17.     while (data != 0.0) {
  18.       sum += data;
  19.       if (data < 0.0) countMinus++;
  20.       if (data > 0.0) countPlus++;
  21.       if (data != 0.0) countAll++;
  22.  
  23.     double average = sum/countAll;
  24.  
  25.       dataString = JOptionPane.showInputDialog(
  26.                                                "Enter a number:\n
  27. (The program will shut down and give you your resault if you press 0 )");
  28.  
  29. data = Double.parseDouble(dataString);
  30.  
  31.       JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers:  " + (average = Math.round(average*100.0)/100.0)));
  32.  
  33.     }
  34.   }
  35. }
  36.  
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.

The code works but it probably isn't well organized nor the best way to this.
I just wanted to say that the easiest way of getting know something is to read about it.
Sep 13 '07 #20
Nepomuk
3,112 Expert 2GB
I did a little research. Here's my code. Tell me how you like it:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ListingBls99 {
  4.   public static void main(String[] args) {
  5.  
  6.     String dataString = JOptionPane.showInputDialog(
  7.                                                     "Enter a number:\n
  8. (The program will shut down and give you your resault if you press 0 )");
  9.  
  10. double data = Double.parseDouble(dataString);
  11.  
  12.     double countMinus = 0.0;
  13.     double countPlus = 0.0;
  14.     double countAll = 0.0;
  15.     double sum = 0.0;
  16.  
  17.     while (data != 0.0) {
  18.       sum += data;
  19.       if (data < 0.0) countMinus++;
  20.       if (data > 0.0) countPlus++;
  21.       if (data != 0.0) countAll++;
  22.  
  23.     double average = sum/countAll;
  24.  
  25.       dataString = JOptionPane.showInputDialog(
  26.                                                "Enter a number:\n
  27. (The program will shut down and give you your resault if you press 0 )");
  28.  
  29. data = Double.parseDouble(dataString);
  30.  
  31.       JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers:  " + (average = Math.round(average*100.0)/100.0)));
  32.  
  33.     }
  34.   }
  35. }
  36.  
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.

The code works but it probably isn't well organized nor the best way to this.
You're code isn't bad, but there are a few things, that you could/should change.
  1. Expand|Select|Wrap|Line Numbers
    1. // Counters don't have to be doubles - it's a waste of space
    2. int countMinus = 0;
    3. int countPlus = 0;
    4. int countAll = 0;
    5.  
  2. Expand|Select|Wrap|Line Numbers
    1. while (data != 0.0) {
    2.    sum += data;
    3.    if (data < 0.0) countMinus++;
    4.    if (data > 0.0) countPlus++;
    5.    if (data != 0.0) countAll++;
    6. }
    7.  
    8. // can be improved to:
    9.  
    10. while (data != 0.0) {
    11.    sum += data;
    12.    if (data < 0.0) countMinus++;
    13.    else if (data > 0.0) countPlus++; // your data can't be positive AND negative
    14.    countAll++; // save a little CPU time - not relevant, but when you're improving your code anyway...
    15. }
    16. countAll--;
    17.  
  3. Changing this to a version, which uses Scanner isn't difficult, and using Swing (the package, that JOptionPane belongs to) before having read about it isn't that a good idea. At the beginning, you should try to use the console.
  4. You nearly had the solution before and now you changed your code completely - that makes me think, that you probably don't understand quite how your code works. Have another good look at it and try to understand.
As r035198x said, read about Java a bit more. There are some nice articles in the "Articles -> Java" Section and you can find a good eBook recommended here.

Greetings,
Nepomuk
Sep 13 '07 #21
JosAH
11,448 Expert 8TB
I did a little research. Here's my code. Tell me how you like it:
I don't like it one bit; no insult intended though. I'm a nitpicker and I'd take all the
'logic' out of the main method and create a separate method for it. I also get
the shivers when I see duplicated code.

I appreciate (I really do) that you managed to figure out a working version but I
still don't like that version one bit. Keep on studying and I appreciate your effiorts.

kind regards,

Jos
Sep 13 '07 #22

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by M.Kamermans | last post: by
4 posts views Thread by ZyPhiX | last post: by
10 posts views Thread by Jon | last post: by
68 posts views Thread by Martin Joergensen | last post: by
22 posts views Thread by MP | last post: by
2 posts views Thread by nick048 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.