# Using FOR and WHILE loops to find average (DUE TONIGHT)

• March 1st, 2013, 03:52 PM
cbh793
Using FOR and WHILE loops to find average (DUE TONIGHT)
The user should be instructed to enter three numbers and add them up by using a for loop to accomplish this. The program should then find the average of the three numbers.

After this is accomplished the program should allow the user to add numbers using a while loop that terminates when the user enters -999. The number of entries the user has placed into the sum should then divide the sum.

Here is the revision, but I still need help.

Code java:

``` public static void main(String[] args) throws FileNotFoundException { PrintWriter prw = new PrintWriter("outfile5B.txt"); Scanner kb = new Scanner(System.in);   String input; int num, sum = 0, average, numAmount; final int DIV = 3;   // Part 1 System.out .println("Do you wish to start?"); input = kb.nextLine(); //the program terminates here, idk why? if (input == "yes"){ for(int count = 1 ;count <= 3; count++){ num = kb.nextInt(); sum += num; average = sum / DIV; // will this give me an average? System.out.println("Sum is: " + sum); System.out.println("Count is: " + count); System.out.println("Your average is: " + average);   } // Part 2 System.out.println("Do you wish to add numbers? If so, enter how many. If not enter -999 to abort."); numAmount = kb.nextInt(); while(numAmount > -999){ System.out.println("Enter your number(s)"); // how do I complete the average here? average = sum / numAmount; System.out.println("Your average is: " + average); } } else if (input == "no"){ System.out.println("You have terminated the program");```
• March 1st, 2013, 04:02 PM
Norm
Re: Using FOR and WHILE loops to find average (DUE TONIGHT)
Quote:

I still need help.

[code=java]
[/code]
to get highlighting and preserve formatting.

Looks like some code is missing at the end.
• March 1st, 2013, 04:24 PM
cbh793
Re: Using FOR and WHILE loops to find average (DUE TONIGHT)
better? there are questions in green
• March 1st, 2013, 04:29 PM
Norm
Re: Using FOR and WHILE loops to find average (DUE TONIGHT)
There is no green in formatted code.

One problem I see is the use of the == operator to compare Strings. You need to use the equals() method.

Another problem is the formatting. The code nested inside {}s should be indented 3-4 spaces to make the code easer to read and understand. The ending } should be inline beneath the start of the line with the pairing {

Another possible problem is integer division. There are no decimal places: 5/2 = 2
If you want decimal places change one of the operands to a float: 5/2.0 = 2.5

What happens when the code is compiled and executed?
• March 1st, 2013, 07:47 PM
JakeM1130
Re: Using FOR and WHILE loops to find average (DUE TONIGHT)
Code :

```PrintWriter prw = new PrintWriter("outfile5B.txt"); Scanner kb = new Scanner(System.in);   String input; int num, sum = 0, numAmount; double average; // made this a double for increased accuracy final int DIV = 3; // no need for this   // Part 1 System.out.println("Do you wish to start?"); input = kb.nextLine(); //the program terminates here, idk why? if (input.equals("yes")){ // Use the .equals command when comparing a string rather than == that was why it was terminating for(int count = 1 ;count <= 3; count++){ num = kb.nextInt(); sum += num; average = (double) sum / count; // divide by count to get the average of numbers entered so far, and type cast to double System.out.println("Sum is: " + sum); System.out.println("Count is: " + count); System.out.println("Your average is: " + average);   } sum = 0; // reinitialize sum as 0 for next loop int ct = 0; // this variable will count the number of integers entered so you can average them while(numAmount > -999){ System.out.println("Enter your number(s)"); numAmount=kb.nextInt(); // put this inside the loop so it can continue to get input sum += numAmount; ct += 1; // how do I complete the average here? average = (double) sum / ct; // computes the new average System.out.println("Your average is: " + average); }```
• March 1st, 2013, 07:53 PM
Norm
Re: Using FOR and WHILE loops to find average (DUE TONIGHT)
@JakeM1130
Quote: