Count input values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dadimar
    New Member
    • Sep 2007
    • 9

    #1

    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...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dadimar
    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.

    Comment

    • dadimar
      New Member
      • Sep 2007
      • 9

      #3
      Originally posted by r035198x
      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 ?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by dadimar
        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

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by dadimar
          Yes I know how to read the input, but how do I count it ?
          The while loop

          Comment

          • dadimar
            New Member
            • Sep 2007
            • 9

            #6
            I have no idea. Am I on the right track:

            import java.util.Scann er;

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

            Scanner read = new Scanner(System. in);

            System.out.prin tln("text: ");

            int number = read.nextInt();

            while (number != 0) {

            }

            System.out.prin tln("Quit");
            }
            }

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dadimar
              I have no idea. Am I on the right track:

              import java.util.Scann er;

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

              Scanner read = new Scanner(System. in);

              System.out.prin tln("text: ");

              int number = read.nextInt();

              while (number != 0) {

              }

              System.out.prin tln("Quit");
              }
              }
              1.) Use code tags for posting code
              2.) Try it on the compiler and see what it does or doesn't do.

              Comment

              • dadimar
                New Member
                • Sep 2007
                • 9

                #8
                Code:
                  
                while (number != 0) {
                number = read.nextInt();
                };
                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.

                Comment

                • dadimar
                  New Member
                  • Sep 2007
                  • 9

                  #9
                  Originally posted by dadimar
                  Code:
                    
                  while (number != 0) {
                  number = read.nextInt();
                  };
                  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?

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by dadimar
                    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

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by dadimar
                      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.

                      Comment

                      • dadimar
                        New Member
                        • Sep 2007
                        • 9

                        #12
                        Originally posted by nepomuk
                        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.

                        Code:
                        while (number != 0) {
                        number = read.nextInt();
                        };
                        System.out.println("Quit");
                        I put 25, 43 and 55 in. If I add System.out.prin tln(number) I get the last number 55.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by dadimar
                          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.

                          Code:
                          while (number != 0) {
                          number = read.nextInt();
                          };
                          System.out.println("Quit");
                          I put 25, 43 and 55 in. If I add System.out.prin tln(number) I get the last number 55.
                          Perhaps now is the time to read a tutorial?

                          Comment

                          • dadimar
                            New Member
                            • Sep 2007
                            • 9

                            #14
                            Code:
                            int count = 1;
                            int number  = read.nextInt();
                            
                            while (number != 0) {
                                number = read.nextInt();
                                if (number != 0) count++; 
                                };
                                System.out.println("Quit....");
                            This is one way to count how often input numbers are put in.

                            Comment

                            • Nepomuk
                              Recognized Expert Specialist
                              • Aug 2007
                              • 3111

                              #15
                              Originally posted by dadimar
                              Code:
                              int count = 1;
                              int number  = read.nextInt();
                              
                              while (number != 0) {
                                  number = read.nextInt();
                                  if (number != 0) count++; 
                                  };
                                  System.out.println("Quit....");
                              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

                              Comment

                              Working...