Count input values?

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

    #16
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #17
      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.
      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:

      [code=java]
      int count = 0;

      for (int number; (number= read.nextInt()) != 0; count++) {
      // your valuable code here using 'number'
      // no need to test for 0 here again.
      }
      [/code]

      kind regards,

      Jos

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #18
        Originally posted by dadimar
        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:
        [CODE=java]
        int count = 1;
        int number = read.nextInt();

        while (number != 0) {
        number = read.nextInt();
        if (number != 0) count++;
        // do something with "number"
        } // You don't need a semicolon here, although it isn't wrong
        System.out.prin tln("Quit....") ;
        [/CODE]or, as JosAH suggested:
        [CODE=java]
        int count = 0;

        for (int number; (number= read.nextInt()) != 0; count++) {
        // your valuable code here using 'number'
        // no need to test for 0 here again.
        }
        [/CODE]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:
        [CODE=java]
        int a = 3;
        a *= 3; // or you can use a = a * 3;
        [/CODE]

        Greetings,
        Nepomuk

        Comment

        • dadimar
          New Member
          • Sep 2007
          • 9

          #19
          I did a little research. Here's my code. Tell me how you like it:

          Code:
          import javax.swing.JOptionPane;
          
          public class ListingBls99 {
            public static void main(String[] args) {
              
              String dataString = JOptionPane.showInputDialog(
                                                              "Enter a number:\n
          (The program will shut down and give you your resault if you press 0 )");
              
          double data = Double.parseDouble(dataString);
              
              double countMinus = 0.0;
              double countPlus = 0.0;
              double countAll = 0.0;
              double sum = 0.0;
              
              while (data != 0.0) {
                sum += data;
                if (data < 0.0) countMinus++;
                if (data > 0.0) countPlus++;
                if (data != 0.0) countAll++;
          
              double average = sum/countAll;
                
                dataString = JOptionPane.showInputDialog(
                                                         "Enter a number:\n
          (The program will shut down and give you your resault if you press 0 )");
                
          data = Double.parseDouble(dataString);
                
                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)));
                
              }
            }
          }
          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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by dadimar
            I did a little research. Here's my code. Tell me how you like it:

            Code:
            import javax.swing.JOptionPane;
            
            public class ListingBls99 {
              public static void main(String[] args) {
                
                String dataString = JOptionPane.showInputDialog(
                                                                "Enter a number:\n
            (The program will shut down and give you your resault if you press 0 )");
                
            double data = Double.parseDouble(dataString);
                
                double countMinus = 0.0;
                double countPlus = 0.0;
                double countAll = 0.0;
                double sum = 0.0;
                
                while (data != 0.0) {
                  sum += data;
                  if (data < 0.0) countMinus++;
                  if (data > 0.0) countPlus++;
                  if (data != 0.0) countAll++;
            
                double average = sum/countAll;
                  
                  dataString = JOptionPane.showInputDialog(
                                                           "Enter a number:\n
            (The program will shut down and give you your resault if you press 0 )");
                  
            data = Double.parseDouble(dataString);
                  
                  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)));
                  
                }
              }
            }
            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.

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #21
              Originally posted by dadimar
              I did a little research. Here's my code. Tell me how you like it:

              Code:
              import javax.swing.JOptionPane;
              
              public class ListingBls99 {
                public static void main(String[] args) {
                  
                  String dataString = JOptionPane.showInputDialog(
                                                                  "Enter a number:\n
              (The program will shut down and give you your resault if you press 0 )");
                  
              double data = Double.parseDouble(dataString);
                  
                  double countMinus = 0.0;
                  double countPlus = 0.0;
                  double countAll = 0.0;
                  double sum = 0.0;
                  
                  while (data != 0.0) {
                    sum += data;
                    if (data < 0.0) countMinus++;
                    if (data > 0.0) countPlus++;
                    if (data != 0.0) countAll++;
              
                  double average = sum/countAll;
                    
                    dataString = JOptionPane.showInputDialog(
                                                             "Enter a number:\n
              (The program will shut down and give you your resault if you press 0 )");
                    
              data = Double.parseDouble(dataString);
                    
                    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)));
                    
                  }
                }
              }
              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. [CODE=java]
                // Counters don't have to be doubles - it's a waste of space
                int countMinus = 0;
                int countPlus = 0;
                int countAll = 0;
                [/CODE]
              2. [CODE=java]
                while (data != 0.0) {
                sum += data;
                if (data < 0.0) countMinus++;
                if (data > 0.0) countPlus++;
                if (data != 0.0) countAll++;
                }

                // can be improved to:

                while (data != 0.0) {
                sum += data;
                if (data < 0.0) countMinus++;
                else if (data > 0.0) countPlus++; // your data can't be positive AND negative
                countAll++; // save a little CPU time - not relevant, but when you're improving your code anyway...
                }
                countAll--;
                [/CODE]
              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

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

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

                Comment

                Working...