# loop help

• 03-08-2011, 02:03 AM
droidus
loop help
i am having trouble with getting the average of numbers that a user enters (will divide later once as i can get the sum of all of their numbers....). they can enter as many as they like. sorry about the brackets lining up... it was perfect in java, but when copied over, it threw everything out of line... :p here is what i have (i have questions inserted):

Code:

```        double input;         double max=0;         double min=0;         double average = 0;         double range = 0;         String message = "";                         int count = 0;                         double numbers = 0;                         double numbersSum = 0;               do         {         //Prompts user for their input //this does not get repeated every time a number is entered         Scanner in = new Scanner(System.in);         System.out.print("What is your input? (enter -1 to terminate this program: ");         input = in.nextDouble();                                   // this gets the first number, and sets it to the min and max variables                                for (count=0; count < 1; count++);                                 {                                         if (count == 0)                                         {                                                 max = input;                                                 min = input; // can't you set input=max and min?                                         }                                 }                                 System.out.println ("numbers:" + max + min);                                                         for (count=0; count < 100; count++); // how do you make the restriction unlimited?                         {                                 numbers = (Character.getNumericValue(input.charAt(count))); //double is being dereferenced... how can i fix this?  also, i don't think i'm doing this right... i'm getting the numbers in each input seperatley, right?                                 numbersSum = numbersSum + numbers; // the sum of all the numbers the user added, so they can be divided by 2 later                         }                                                  System.out.print ("the numbers added up are: " + numbersSum + ".");                                         average = ((numbersSum)/2);                     range = (max-min);                                 // this updates the max value         while (in.hasNextDouble())         {                 input = in.nextDouble();                                         if (input > max)                 {                         max = input;                 }         }                                                 // this updates the min value                         while (in.hasNextDouble())         {                 input = in.nextDouble();                   if (input < min)                 {                                 min = input;                 }         }               }                 while (input != -1);               System.out.println("The average of these two inputs is: " + average + ".");         System.out.println("The range of these two inputs is: " + range + ".");         System.out.println(message);```
• 03-08-2011, 03:53 AM
sunde887
Don't do any math or anything to start. Start small and work your way up. There are two solid ways to implement this. One is to use an arrayList<Integer>, and the other is to ask the user how many items they want to add.

lets go over the first method(using an array list)
You create an array list, and then set up an infinite list and inform them of when to input to end the loop. If they enter that number/symbol/etc break out of the loop. Else you will add the input to the array list until they enter the breakable number

the other method would be to first ask them for an array size and then initialize the array to be that size, then loop that many times and continually add the user inputs until the size is reached.
• 03-08-2011, 03:58 AM
Junky
Code:

```for (count=0; count < 1; count++); // <-- YIKES! {     if (count == 0)     {         max = input;         min = input;     } }```
If you think very carefully about this you will realise that the for loop and if statement are totally pointless.
Quote:

// can't you set input=max and min?
Yes, exactly how you have done it.
Quote:

// how do you make the restriction unlimited?
Use for loops when you have a finite number of iterations. Use while loops when you have infinite number of iterations.
Quote:

//double is being dereferenced
Primitives do not have methods. Think about which class does have the charAt method.
Quote:

// the sum of all the numbers the user added, so they can be divided by 2
Really? According to you the average of 5, 5, 5, 5, 5, 5 is 15 and not 5.
• 03-08-2011, 02:13 PM
droidus
hm, haven't learned about arrays yet...

and for some reason, the while statement (while (in.hasNextDouble())) throws off my loop. after entering a number, it only continues the program, but does not continue to prompt the user for their next number. is this because it could be stuck in one of the loops?
• 03-08-2011, 05:41 PM
sunde887
don't use hasNextDouble. This is possible with out arrays.

There are two ways to do this, the first is to ask them how many numbers they would like to use(then use either a for or while loop) and prompt them for numbers n times(where n is the first number you prompted for)

Each time through the loop you should add the input to a sum variable.

Code:

```declare sum declare counter prompt for amount of numbers use input in loop condition loop(condition)   prompt   get input   add to sum end loop```
Just do this first.

You can also set up an infinite loop and give the user a letter/word that breaks out of the loop. With this method you will also have to keep track of how many items they input.
• 03-08-2011, 05:48 PM
JosAH
Quote:

Originally Posted by sunde887
Don't do any math or anything to start.

Why not? A bit of math up front never hurts. At least it avoids all those cripple programs that use loops etc. that aren't even necessary.

kind regards,

Jos
• 03-08-2011, 05:51 PM
droidus
here is what i have so far (by the way, i am doing the last thing you mentioned, but i don't think she wants us to have to enter how many numbers we will be entering...) my biggest issue right now, is that the min and max values do not get set properly. also, how can i count how many numbers the user enters? i tried a counter here, but it doesn't work out so well.

Code:

```                        double min = 0;                 double max = 0;                 double input = 0;                 int count = 0;                 double range = 0;                 int average = 0;                                 do                 {                         for (count=0; count<1; count++)                         {                                 min = input;                                 max = input;                         }                                                  for (count = 0; count>0; count++)                         {                                 count = count++;                         }                                                 //Prompts user for their input //errors here         Scanner in = new Scanner(System.in);         System.out.print("What is your input? (enter -1 to terminate this program: ");         input = in.nextDouble();                         if (input<min)                         {                                 min = input;                         }                                                 if (input>max)                         {                                 max = input;                         }                 }                                 while (input != -1);                                 range = max - min;                                 System.out.println ("The minimum value of all the points you entered is: " + min + ".");                 System.out.println ("The maximum value of all the points you entered is: " + max + ".");                 System.out.println ("The range is: " + range + ".");                 System.out.println ("The average value of all the points you entered is: " + average + ".");                 System.out.println ("The count is: " + count + ".");```