cris0530
cris0530

Reputation: 1

Java - How do I continue to ask the user for input?

I've been at this program for a couple of hours and I still don't understand the logic. The program asks the user to enter integers.

Once the user has entered the integers it will output to the user all the positive numbers, negative numbers and total of numbers entered, along with the average for the numbers.

Here is where I'm stuck:

Then it will ask the user if they want to continue. The user will enter 'y' or 'n' to indicate if they do or do not want to continue. If the user enters 'y' the loop will go again, if they enter 'n' it will display a goodbye message. I don't know how to set that up.

I've tried a do while loop and I am currently using a while loop for the program. I'm also missing a statement that lets the user know that they only entered zero if no other numbers were entered. I don't know how to set that up either but I have tried an if statement and it doesn't work.. My code is below. I am using Java 8.

public class CountPositiveNegativeAndAverageOfIntsProgram {

public static void main(String [] args)
{

     System.out.print("Enter an integer, the input ends if it is 0: ");
     //Creating a scanner object
    Scanner input = new Scanner(System.in);
    int userInput = input.nextInt();

    //creating variables that will be used in the program.
    int countPositive = 0;
    int countNegative = 0;
    int total = 0;
    double average = 0;

    while(userInput != 0)
    {            

        if(userInput > 0)
        {
        //using a counter to add how many positive numbers are entered.
        countPositive++;
        }
        else
        {
          countNegative++;      
        }

        total += userInput;
        //I added 0.0 to avoid integer division. This felt like a small hack.
        average = total / (countPositive + countNegative + 0.0);
        userInput = input.nextInt();

    }      

     System.out.println("The number of positives is: " + countPositive);
     System.out.println("The number of negatives is: " + countNegative);
     System.out.println("The total is: " + total);
     System.out.println("The average is : " + average);

     System.out.print("Continue? (y/n) ");
     String s = input.next();




}

}

Upvotes: 0

Views: 1956

Answers (1)

Jason
Jason

Reputation: 5246

A solution, granted not a good one, is just to create a function in which you can call recursively when or if the user provides y as a response.

    public static void main(String [] args) {
        request();
    }

    private static void request() {
        System.out.println("Enter an integer, the input ends if it is 0: ");
        //Creating a scanner object
        Scanner input = new Scanner(System.in);
        int userInput = input.nextInt();

        //creating variables that will be used in the program.
        int countPositive = 0;
        int countNegative = 0;
        int total = 0;
        double average = 0;

        while(userInput != 0)
        {

            if(userInput > 0)
            {
                //using a counter to add how many positive numbers are entered.
                countPositive++;
            }
            else
            {
                countNegative++;
            }

            total += userInput;
            //I added 0.0 to avoid integer division. This felt like a small hack.
            average = total / (countPositive + countNegative + 0.0);
            userInput = input.nextInt();

        }

        System.out.println("The number of positives is: " + countPositive);
        System.out.println("The number of negatives is: " + countNegative);
        System.out.println("The total is: " + total);
        System.out.println("The average is : " + average);

        System.out.println("Continue? (y/n) ");

        String response = input.next();

        if (response.equals("y")) {
            request();
        } else {
            System.out.println("Goodbye!");
        }
    }

Java 8 Solution If you really wanted to improve this a bit you could do a few things.

  • Use Scanner#hasNextInt to determine if the scanner has an Int to consume.
  • Instead of using multiple variables to track negative, positive, and total, maintain a List of Integer objects representing input.
  • Use Stream#filter to check for positive and negative values, followed by count() to tell us the number of elements in the Stream that match this filter.
  • Use IntStream#sum and IntStream#average to determine the sum and average of the Stream of Integers respectfully.
    private static void request(Scanner scanner) {
        System.out.println("Enter an integer, the input ends if it is 0: ");

        List<Integer> values = new ArrayList<>();

        while(scanner.hasNextInt()) {
            int value = scanner.nextInt();

            if (value == 0) {
                break;
            }
            values.add(value);
        }
        System.out.println("The number of positives is: " + values.stream().filter(value -> value > 0).count());
        System.out.println("The number of negatives is: " + values.stream().filter(value -> value < 0).count());
        System.out.println("The total is: " + values.stream().mapToInt(value -> value).sum());
        System.out.println("The average is : " + values.stream().mapToInt(value -> value).average().orElse(0));
        System.out.println("Continue? (y/n) ");

        String response = scanner.next();

        if (response.equals("y")) {
            request(scanner);
        } else {
            System.out.println("Goodbye!");
        }
    }

Upvotes: 1

Related Questions