System.out.print-method doesn´t work right
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)开发者_StackOverflow社区;
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
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();
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()
精彩评论