odhinnhrafn
odhinnhrafn

Reputation: 3

How to close a while loop using user input for doubles

So, disclaimer, this is for a class, but I've already submitted the homework and reached out to the professor for assistance, so I'm not cheating or asking someone to do my homework. I spent hours trawling through this site, in the process, as well.

I am trying to get user input for up to 52 times, add them all together and then average the sum.

I made this method to allow the user to input their weekly profits, based on the Sales variable input further up in the code. I thought a limit of 52 would be good, as it would allow a person to average 2 wks, a month, 6 months, etc, like if they’re trying to budget or find an estimated yearly earnings.

    //new method
    public static void computeAverageSalesProfit() {       

        //new scanner object in method
        Scanner input = new Scanner(System.in);

        //new object
        Sales AvgProfit = new Sales();

        //prompt for input
        System.out.println("Please enter weekly profits, up to 52 weeks.  When finished, enter '-1.'");       

        //value to store input, and test against
        double value = 0;
        //new double array to sum together and average out
        double[] sum2 = new double[52];

        //while loop of doom
        while(value >= 0) {

        for (int i=0; i<sum2.length; i++) {
        value = input.nextDouble();
        sum2[i] = value + 0;

        }
        }

        double WeeklyProfits = 0;
        for (int i=0; i<sum2.length; i++) {
            WeeklyProfits = sum2[i] / sum2.length;
        }

        AvgProfit.setWeeklyProfits(WeeklyProfits);

        System.out.printf("Your average profits are %.2f", AvgProfit.getWeeklyProfits());

    }

This is the code I ended with for the night, and I cannot figure out how to end the while loop. I didn’t originally have the value variable, but thought that a check for it before the sum2 iteration would allow me to close it. When I run the program, it takes in user input, but never ends until I hit 52 iterations. I also tried using "0000" in a boolean expression, but that didn't work, either.

I also tried without setting the value variable, I’ve tried every way I can think of and can find on the internet. I tried with and without a value variable with just a plain double (not array). I even tried with a String, using a boolean expression (good = true/false, while (!/good), etc, but I was unable to convert to double and average it out.

I’m completely baffled, and my head hurts. I would really appreciate any assistance or insight you’re able to share. Thank you.

Oh, and I have a while loop in a different part of my code that does work, but I don't think this one works the same way. I think I'm either overthinking it, or just missing something very obvious.

//Check Year length separate, keeping original code mostly intact
    public static boolean isValid(String input)
    {
        //Does it have 4 digits?
        if(input.length() != 4)
            return false;

    // Is it a number ?
        try
        {
            Integer i = Integer.parseInt(input);
        }
        catch(NumberFormatException e)
        {
            return false;
        }

        // Passed all checks and is valid
        return true;
    }    

Upvotes: 0

Views: 330

Answers (1)

Henrique Sabino
Henrique Sabino

Reputation: 545

One thing that you could do is to instead of using a while loop use the for loop like this:

//value to store input, and test against
double value = 0;
//new double array to sum together and average out
double[] sum2 = new double[52];

// repeats 52 times max
for (int i=0; i<sum2.length; i++) {
    value = input.nextDouble();

    // Checking if the user entered a negative number
    if (value < 0) {
        // The break keyword exits a loop
        break;
    }

    sum2[i] = value + 0;
}

Upvotes: 1

Related Questions