How Can I Get This While Loop to Run When Using a Sentinel Value for a Variable with Scanner Input Inside of the Loop, While Calling Methods?

2 months into my first java programming. I'm trying to create a while loop in a class with methods and scanner inputs. Attempting to use a sentinel value to break out of the while loop. I've attempted while loops, do-while loops, with and without if-else blocks.

It looks like it's requiring me to initialize the salePrice for it to work, but the salePrice is entered by the user inside the loop. I have tried to assigning a fake value to it before the while loop, it then lets me start the loop, but 0 does not break of out of it, it just goes to the second question in the loop.

Is there something fundamentally wrong with this logic structure? Do the methods create a different need? I'm only 2 months into coding, so it has to be a very simple answer... Do I need to put if-else logic within the while loop just to break out of the loop with a sentinel value in this case?

public class PurchaseCalculator {

    //create main method
    public static void main(String[] args) {
        // declare variables
        // double salePrice;
        double discount = .15;
        double maxPurchaseAmount;
        double salePrice;

        // create scanner object
        Scanner userInput = new Scanner(System.in);
       
        while (salePrice > 0){
            // prompt user to enter price
            System.out.println("Enter the price of the item. Enter 0 to end program"); 
            // read in price from user and store in sale price
            salePrice = userInput.nextDouble();
        
            //create a scanner object for salesPrice input
            System.out.println("Price entered: $" + salePrice);
            //prompt user to enter max purchase amount
            System.out.println("Enter the maximum purchase amount");
            // read in max amount from user and store in maxPurchaseAmount
            maxPurchaseAmount = userInput.nextDouble();
            
            // call displaypricewithtax method
            displayPriceWithTax(salePrice);
            // call computePriceAfterDiscountMethod
            computePriceAfterDiscount(salePrice, discount);
            // call displaypurchasablelimit method
            displayPurchasableNumber(salePrice, maxPurchaseAmount);
        
        } // close while loop
    } //close main
1

1 Answer

In Java any local variable1 must be initialized before its value can be read. Not to be confused with class fields2, which are initialized with default values.

1 - variables declared inside statement blocks ({...})
2 - instance variables or class variables


Option 1: change the while loop to a do-while loop. Using this, the check is done after the iteration, so a value is assigned to the variable:

double salePrice;

do {
    ...
    salePrice = userInput.nextDouble();
    ...
while (salePrice > 10);

To avoid execution of the rest of the iteration, an if can be used; but mostly the next option (Option 2) is used instead:

double salePrice;

do {
    ...
    salePrice = userInput.nextDouble();
    if (salePrice >0) {
        ...
    }
while (salePrice > 10);

Option 2: use an if inside the loop:

while (true) {    // for ever
    ...
    salePrice = userInput.nextDouble();
    if (salePrice <= 0)
        break;
}

Option 3: use a flag (boolean) to control the loop

boolean stop = false;
while (!stop) {
    ...
    salePrice = userInput.nextDouble();
    stop = salePrice <=0;
    ...
}

Option 4: assign a fake initial value:

double salePrice = 1.0;
while (salePrice > 0) {
    ...
    salePrice = userInput.nextDouble();
    ...
}

(I would suggest going by first or second option, eventually 3rd for some cases)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest