By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

Program is printing 2 lines instead of 1 line

trkrbabe
P: 3
Hi, this is my first time posting here. It appears that I am taking the same class as a few other people here. I have only been learning Java for about five weeks now.

I have my Product class compiled and I finally got my Inventory class compiled but when I run the program it prints the first line correctly then I input the item name but then it prints the next two lines when it should print one and allow me to input then print the next.

Here is my code:

// Product.java
// A class to store product information

public class Product
{
public String itemName; // item name
public int itemNumber = 0; // item number
public double itemTotal = 0; // number of items
public double itemPrice = 0; // price of the item;
public double valueInventory = 0;

// constructor to initialize the itemName
public void Product( String name )
{
itemName = name; // initialize itemName
} // end constructor

// method to calculate value of inventory
public double calculateValueInventory()
{
valueInventory = itemTotal * itemPrice;
return valueInventory;
} // end method

} // end public class Product


// Inventory5.java
// A program that displays inventory information

import java.util.Scanner; // program uses class Scanner
import java.lang.String;
import java.lang.Object;
import java.io.InputStream;

public class Inventory5
{
public static void main( String args[] )
{
// create Product object
Product item = new Product();

// create Scanner to obtain information
Scanner input = new Scanner( System.in );

// obtain input from user
while ( true )
{
System.out.println( "Enter the item name: "); // prompt for item name
item.itemName = input.next(); // read a line of text

if ( item.itemName.equalsIgnoreCase( "stop" ) ) // to end the while
break;

System.out.println( "Enter the item number: "); // prompt for item number

item.itemNumber = input.nextInt(); // read item number

System.out.println( "Enter the item price: "); // prompt for item price
item.itemPrice = input.nextDouble(); // read item price

System.out.println( "Enter the item total: "); // prompt for total amount of items
item.itemTotal = input.nextDouble(); // read item total

// call calculateValueInventory method
item.calculateValueInventory();
// end method

System.out.printf( "Item name is %s\n", item.itemName); // display item name

System.out.printf( "Item number is %d\n", item.itemNumber); // display item number

System.out.printf( "Item price is %.2f\n", item.itemPrice); // display item price

System.out.printf( "Number of items in stock: %.2f\n", item.itemTotal); // display item total

System.out.printf( "Value of inventory is %.2f\n", item.valueInventory); // display inventory value

} // end while

} // end main method

} // end class Inventory5

Any help would be appreciated as to why it is printing both lines at the same time. Thank you!
Aug 5 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
When you read your last number in that loop body; the Scanner attempts to
read that number for you, and because you did type in a number, it succeeds
and stops when it 'sees' that new line character; it won't read that new line
character.

The next pass around you attempt to read a name, a String; the Scanner sees
that new line character and assumes that an empty String must be read.

Got it? After you have read that last number in the first loop pass you have to
get rid of that new line character. Simply invoking scanner.readLine() will take
care of that. Put that method call as the last statement of the loop body.

kind regards,

Jos
Aug 5 '07 #2

trkrbabe
P: 3
Thank you for replying. When I run the program from the command prompt; it prints the line to "Enter item name" so then I input the item name and press enter, it then prints out

Enter the item number:
Enter the item price:

For some reason, the program prints out both lines without me first input information for the item number.

I also moved the method call to the end of the loop. Thank you!
Aug 5 '07 #3

Expert 10K+
P: 11,448
Thank you for replying. When I run the program from the command prompt; it prints the line to "Enter item name" so then I input the item name and press enter, it then prints out

Enter the item number:
Enter the item price:

For some reason, the program prints out both lines without me first input information for the item number.
I don't understand that behaviour; what was the name your typed? Are you
sure you didn't type 'John 123' or something?

kind regards,

Jos
Aug 5 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.