0

I am having issues with a problem that I coded for a Java course I am taking, and I cannot figure out why it is behaving a certain way. Here is the problem from the book, both part A and part B:

A) Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase's details. Save the file as Purchase.java

B) Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1000 and 8000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative number, sale amount, and sales tax. Save the file as CreatePurchase.java.

Here is the code for the first part of the problem:

public class Purchase
{

    int invoiceNumber = 1234;
    double salePrice = 10.00;
    double SalesTax;

    public void setInvoiceNumber(int invoice)
    {
        invoiceNumber = invoice;
    }

    public void setSalePrice(double saleAmount)
    {
        salePrice = saleAmount;
        SalesTax = (saleAmount * .05);
    }

    public void displaySalePrice()
    {
        System.out.println("Your invoice number is:" + invoiceNumber + ".");
        System.out.println("Your sale amount is: " + salePrice + ".");
        System.out.println("Your sales tax is: " + SalesTax + ".");
    }

 }

Here is the code for the second part of the problem:

import java.util.Scanner;

public class CreatePurchase
{
    public static void main(String[] args)
    {

        int invoice;
        double saleAmount;
        invoice = 0;
        saleAmount = 0.0;
        Purchase completedPurchase = new Purchase();

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the invoice number: ");
        invoice = input.nextInt();
        System.out.println("Please enter the sale amount: ");
        saleAmount = input.nextDouble();

        do
        {
            System.out.println("You entered an invalid number.");
            System.out.println("Please enter a number between 1000 and 8000.");
            invoice = input.nextInt();
        }   
        while (invoice < 1000 || invoice > 8000);

        do
        {
            System.out.println("You entered an invalid number."); 
            System.out.println("Please enter a number greater than 0.");
            saleAmount = input.nextDouble();
        }
        while (saleAmount < 0);

        completedPurchase.setInvoiceNumber(invoice);
        completedPurchase.setSalePrice(saleAmount);

        completedPurchase.displaySalePrice();
    }
}

When I compile CreatePurchase.java and run it, it works, but has to cycle through the loops first before it works. For instance, I will type in 7000 for the invoice value and 100 for the sale amount. Those two values should automatically call the completePurchase.displaySalePrice(); method because the invoice number is greater than 1000 and less than 8000, and the sale amount is greater than 0. That being the case, it still cycles through the do while loops once before calling that method.

I cannot for the life of me figure this out. It's probably something pretty simple I am missing. Any help would be greatly appreciated.

After the great guidance of everyone below, I changed the code for the loops to the following:

while (invoice < 1000 || invoice > 8000)
        {
            System.out.println("You entered an invalid number.");
            System.out.println("Please enter a number between 1000 and 8000.");
            invoice = input.nextInt();
        }   

        while (saleAmount < 0)
        {
             System.out.println("You entered an invalid number."); 
             System.out.println("Please enter a number greater than 0.");
             saleAmount = input.nextDouble();
        }

It still isn't working correctly. Changing the loops to while loops certainly worked, but now when I enter a number for the invoice number that's in the correct range and an incorrect number for the saleAmount, the program finished and does not execute the while loop for saleAmount? I seem to be missing a concept here.

Also, can anyone recommend a better IDE than JGrasp. That is what we were told to use, but it's cumbersome. I have VisualStudio and Eclipse, but I feel that doing java homework in those two IDE's might be overkill. I will be taking more java courses and c++ courses, so maybe it's worth learning the basics in VS or Eclipse. Any suggestions would be appreciated.

3
  • Hint: There are two types of while loops Commented Dec 2, 2011 at 22:47
  • With a do...while execution enters the scope of do{} at least once ("do this and for the next loops take this condition into account"). Also make sure you know what the || (conditional OR) operator is doing. It is working as expected with your example input but only because the first expression is being evaluated as false. Commented Dec 2, 2011 at 22:58
  • 1
    All of the advice to use while loops instead of do while loops was accurate. I change the loops from do while to while loops. That immediately corrected the issue. I had some other minor mistakes that I corrected and the program is working correctly. Thank you everyone for your insight and assitance. Commented Dec 3, 2011 at 0:12

9 Answers 9

2

That is because the do block always gets executed at least once. You should use a while loop instead:

while (invoice < 1000 || invoice > 8000)
{
  System.out.println("You entered an invalid number.");
  System.out.println("Please enter a number between 1000 and 8000.");
  invoice = input.nextInt();
} 

This way, you only ask for another number, if the invoice number is not between the range you defined.

Sign up to request clarification or add additional context in comments.

Comments

0

That's how do...while loops work: the body is executed once, and then the condition is checked. Use a normal while loop instead.

Comments

0

It's because you're using do{ } while(...) instead of while(...) { }

The do while is guaranteed to run at least once, and then continue looping.

The while will never run if the initial condition is false.

Comments

0

In do-while loops, the body is executed before the condition is tested. You probably want to be using while loops, where the body is executed after the condition is tested.

I highly suggest you read about the difference from Sun's while and do-while tutorial.

Comments

0

Change your loops to whiles

while (invoice < 1000 || invoice > 8000)
{
    ...
}

Comments

0

When I complie CreatPurchase.java and run it, it works, but has to cycle through the loops first before it works.

That's because you're using a do...while loop. A do...while loop executes at least once, and checks the condition after executing the body of the loop.

What you want in this case is a simple while loop, so that the condition is checked before executing the body of the loop. ie:

while (invoice < 1000 || invoice > 8000)
{
    ...
}   

Comments

0

There are several loop functions in Java, one is the do-while-loop which always executes once and then if the while-part is true it loops again.

Another, and better in this case, is the while-loop that works the same but checks the while-part before the first loop.

Check out the tutorial

Comments

0

You need a while (condition) {…} loop. This way the code in the loop will be executed only if the condition is true. In your code you have a do … while(condition) loop and the inside of the loop will always be executed at least once.

Comments

0
import java.util.Scanner;

public class CreatePurchase {

public static void main(String[] args) {

    int invoice;
    double amount;

    Purchase sale1 = new Purchase();
    System.out.println("Enter invoice number between 1000 and 8000 >>");
    Scanner input = new Scanner(System.in);
    invoice = input.nextInt();

    while (invoice < 1000 || invoice > 8000) {
        System.out.println("You entered a wrong invoice number");
        System.out.println("Enter invoice number between 1000 and 8000 >>");
        invoice = input.nextInt();
    }

    System.out.println("Enter sales amount >>");
    amount = input.nextDouble();

    while (amount < 0) {
        System.out.println("Enter number greater than 0 ");
        System.out.println("Enter sales amount >>");
        amount = input.nextDouble();
    }

    sale1.setInvoiceNumber(invoice);
    sale1.setAmount(amount);
    sale1.display();
}
}

1 Comment

What's an answer without a little summary/explanation?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.