1

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

user820913
  • 623
  • 3
  • 11
  • 21

2 Answers2

2

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();
samack
  • 775
  • 10
  • 28
  • Glad that worked for you. Please make sure to mark as answer on your questions so people get credit for answering :). I see you have not marked any answers on your questions. Thank you. – samack Sep 07 '11 at 17:36
1

Basically, the problem is that when you call nextInt/nextDouble, those functions only grab the number part of what is actually present in your input stream. They don't read the enter character that hitting the "enter" key put there. Just put a input.nextLine(); after every input.nextInt(), etc.

This appears to be the problem you are seeing: Using scanner.nextLine()

Community
  • 1
  • 1
Amanduh
  • 1,173
  • 1
  • 12
  • 21