-3

I want the user to stop entering numbers whenever they feel like it. When I run it, it does not work the way I want it to. Thanks for all the help in advance.

private static void whileLoop3()
    {
            System.out.printf("%n%nExecuting whileLoop3...%n%n");

            int count=0;
            int total=0;
            int average;
            int temp;

            //while loop 10 times
            while(count < 10)
            {
                //input number from user
                temp = input.nextInt();
                //add to total
                total += temp; //same as total = total + temp
                count++; //same as count = count + 10
            }

            System.out.printf("Count is %d%n", count);
            average = total/count;
            System.out.printf("The average of your numbers is %d%n", average);


            System.out.printf("%nEnter your next number or \"S\" to stop: ");

    }
  • 4
    "When I run it, it does not work the way I want it to." Well, that's going to be true of almost all SO questions. Please be more specific. (Hint: if the user can enter "S" instead of a number, then `nextInt` is going to fail...) – Jon Skeet Mar 01 '17 at 17:59
  • 1
    have you tried to evaluate your exit value inside the loop? – efekctive Mar 01 '17 at 18:00
  • 3
    What is your question? Why isn't your last print statement inside the loop? I don't see any coding effort to deal with a "S" being entered. – tnw Mar 01 '17 at 18:00
  • 1
    Please define "doesn't work" (more info at: http://importblogkit.com/2015/07/does-not-work/). – Pshemo Mar 01 '17 at 18:00
  • You should take a look at these questions: [Validating input using java.util.Scanner](http://stackoverflow.com/q/3059333/1393766), [How to handle infinite loop caused by invalid input using Scanner](http://stackoverflow.com/q/3572160/1393766). [How do I keep a Scanner from throwing exceptions when the wrong type is entered?](http://stackoverflow.com/q/2496239/1393766). – Pshemo Mar 01 '17 at 18:09

1 Answers1

0

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    whileLoop3();
}

static Scanner input = new Scanner(System.in);

private static void whileLoop3() {
    System.out.printf("%n%nExecuting whileLoop3...%n%n");

    System.out.printf("%nEnter your next number or \"S\" to stop: ");

    System.out.println();

    int count = 0;
    int total = 0;
    int average = 0;
    String strTemp = "";
    int temp;

    // while loop 10 times
    while (!strTemp.equals("S")) {
        // input number from user

        if (strTemp.equals("")) {
        } else {

            temp = Integer.parseInt(strTemp);

            // add to total
            total += temp; // same as total = total + temp
            count++; // same as count = count + 10
        }
        strTemp = input.nextLine();
    }

    System.out.printf("Count is %d%n", count);
    if (count != 0)
        average = total / count;
    System.out.printf("The average of your numbers is %d%n", average);

}

}

LaurensVijnck
  • 361
  • 3
  • 16