Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:41:12+00:00 2026-05-27T05:41:12+00:00

I am having issues with a problem that I coded for a Java course

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:41:12+00:00Added an answer on May 27, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still having issues with this problem. Please help if you can. So I am
I'm having a problem using UpdateModel(theModelToUpdate) causing concurrency issues. Basically whats happening is, there
I am working on a problem for homework in a Java Programming course, and
Hope you can help me with this problem. I am having issues with the
I have a huge web app that is having issues with memory leak in
I'm having a problem with Android's MediaPlayer in that it is too slow when
This seems like a simple problem, but somehow I'm having issues with this code.
I'm having issues with a java class project. The first step consist in drawing
Our build person was having issues compiling some source code that is checked into
How do you deal with having only single inheritance in java? Here is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.