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!
3 2075
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
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!
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: S. Staats |
last post by:
Good day, everbody.
Here is a simple program called test.py:
#!/usr/bin/python
print "No newline, please.",
# End of program
Here is what program does:
prompt> ./test.py
No newline, please.
|
by: findmadhav |
last post by:
I need a program in C (something like a TSR) which will automatically
press the function key F6, say about every 5 seconds. Can anyone
provide me with an exe of such a program?
Thanks in advance.
|
by: Patrick De Ridder |
last post by:
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.
Many thanks,
Patrick.
using System.ComponentModel;
using...
|
by: Jeff B. |
last post by:
Has anyone come across a decent algorithm for implementing word wrap
features in .net printing? I have a small component that uses basic
printing techniques (i.e. e.Graphics.DrawString in a...
|
by: eric |
last post by:
I have a text file that I would like to print. I load the contents of
this text file into a string, call it strData. I then pass strData
into my print method, which creates it as a StreamReader. ...
|
by: Grzesiek |
last post by:
Hi
I have Main.cpp and Hello.cpp files in my Dev C++ project.
Main.cpp
#include "Hello.cpp"
int main(){
return 0;
|
by: wshaer |
last post by:
Hi all,
I have an assignment and I need some help with it. This is the assignment
//
Research reports are often required to conform to a given standard such as APA or MLA. These standards...
|
by: daimler007 |
last post by:
dear friends...
how to stop the printer after printing the last line of a report insted of rolling to tha last line...?
Suppose A4 size report is created but the data retrieved by the report...
|
by: Germaris |
last post by:
Hi there!
I implemented a printing function which is working nice even for multiple pages.
Problem is as follows...
Say we have a text 100 lines long.
Each page can hold 80 lines.
We should...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |