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 6477279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:55:46+00:00 2026-05-25T06:55:46+00:00

I am doing excercises in a book called Java, how to program. I have

  • 0

I am doing excercises in a book called “Java, how to program”. I have created a small program with 2 classes. The program is supposed to be used by a hardware store to represent an invoice for items sold. It is supposed to includ 4 pieces of information: A string value for the items number, a string value which describes the product, an int value for the quantity of items sold, and a double value for the items price. I have created 2 objects of the class in a class which contains the main method. I am supposed to use “set and get-methods” for each instance variables.

The problem is that when the programs prompts the user to write the values of the variables, it doesn´t read the first value for the variable “second items number” (Line 5 in the copy of the command window under). I really can´t read in the code why this happens. Can anyone please help me?

The code of the two classes are as follows:

public class aInvoice
    {
    private String number;
    private String description;
    private int quantity;
    private double price;

    public aInvoice(String pNumber, String pDescription, int pQuantity, double pPrice)
        {
        number = pNumber;
        description = pDescription;
        if (pQuantity < 0)
        {quantity = 0;}
        else
        {quantity = pQuantity;}
        if (pPrice < 0)
        {price = 0;}
        else
        {price = pPrice;}
        }

    public String getNumber()
        {
        return number;
        }
    public String getDescription()
        {
        return description;
        }
    public int getQuantity()
        {
        return quantity;
        }
    public double getPrice()
        {
        return price;
        }

    double totalAmount;
    public double getaInvoiceTotalAmount()
        {
        return quantity * price;
        }
    }

and:

    import java.util.Scanner;
    public class aInvoiceTest
    {
    public static void main(String[]args)
        {
        String partNumber1 = null;
        String partDescription1 = null;
        int partQuantity1 = 0;
        double partPrice1 = 0.0;
        String partNumber2 = null;
        String partDescription2 = null;
        int partQuantity2 = 0;
        double partPrice2 = 0.0;

        Scanner input = new Scanner (System.in);
        System.out.print( "Enter first items number: ");
        partNumber1 = input.nextLine();
        System.out.print( "Enter description: ");
        partDescription1 = input.nextLine();
        System.out.print( "Enter quantity: ");
        partQuantity1 = input.nextInt();
        System.out.print( "Enter price: $");
        partPrice1 = input.nextDouble();
        System.out.print( "Enter second items number: ");
        partNumber2 = input.nextLine();    
        System.out.print( "Enter description: ");
        partDescription2 = input.nextLine();
        System.out.print( "Enter quantity: ");
        partQuantity2 = input.nextInt();
        System.out.print( "Enter price: $");
        partPrice2 = input.nextDouble();

        aInvoice aInvoice1 = new aInvoice(partNumber1, partDescription1, partQuantity1, partPrice1);
        aInvoice aInvoice2 = new aInvoice(partNumber2, partDescription2, partQuantity2, partPrice2);

        System.out.printf( "\n\nPart 1´s item number: %s\nItem description: %s\nQuantity: %d\nPrice each: $ %.2f\n\n", aInvoice1.getNumber(), aInvoice1.getDescription(), aInvoice1.getQuantity(), aInvoice1.getPrice () );

         System.out.printf( "\n\nPart 2´s item number: %s\nItem description: %s\nQuantity: %d\nPrice each: $ %.2f\n\n", aInvoice2.getNumber(), aInvoice2.getDescription(), aInvoice2.getQuantity(), aInvoice2.getPrice () );

        System.out.printf( "Total amount: $ %.2f\n\n", (aInvoice1.getaInvoiceTotalAmount() + aInvoice2.getaInvoiceTotalAmount()));
        }
    }

THe reading in the command window is:

Enter first items number: 44
Enter description: pc
Enter quantity: 1
Enter price: $10
Enter second items number: Enter description: phone
Enter quantity: 1
Enter price: $100

Part 1´s item number: 44
Item description: pc
Quantity: 1
Price each: $ 10.00

Part 2´s item number:
Item description: phone
Quantity: 1
Price each: $ 100.00

Total amount: $ 110.00

  • 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-25T06:55:46+00:00Added an answer on May 25, 2026 at 6:55 am

    This is because after you read input using input.nextInt() or input.nextDouble() you need to clear the buffer before trying to read in another string.

    When the user types in 5.0 the 5.0 is taken from the input buffer but the carriage return is left. You can put a input.nextLine() and this will clear that carriage return for you.

    so your code should look like

        System.out.print( "Enter price: $");
        partPrice1 = input.nextDouble();
    
        input.nextLine();
    
        System.out.print( "Enter second items number: ");
        partNumber2 = input.nextLine();    
        System.out.print( "Enter description: ");
        partDescription2 = input.nextLine();
        System.out.print( "Enter quantity: ");
        partQuantity2 = input.nextInt();
        System.out.print( "Enter price: $");
        partPrice2 = input.nextDouble();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing the first chapter exercises on my Java book and I have been
I'm trying out several exercises from a Java programming book. I have the code
I'm doing a book exercise that says to write a program that generates psuedorandom
i'm doing some exercises in my Java book. I'm very new to programming. Therefore,
i am doing an exercise from Orielly book, and they used that Twitter package
I am a beginner in Java and am doing an exercise from a book.
I am doing a book exercise regarding the Ackermann function. I have one question
I was doing an exercise on F# Wiki Book on List (scroll to the
I am doing this small exercise. declare @No decimal(38,5); set @No=12345678910111213.14151; select @No*1000/1000,@No/1000*1000,@No; Results
Guys, I'm doing excercises from The C++ Programming Language 3rd ed. and on page

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.