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 calculateValueI nventory()
{
valueInventory = itemTotal * itemPrice;
return valueInventory;
} // end method
} // end public class Product
// Inventory5.java
// A program that displays inventory information
import java.util.Scann er; // program uses class Scanner
import java.lang.Strin g;
import java.lang.Objec t;
import java.io.InputSt ream;
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.prin tln( "Enter the item name: "); // prompt for item name
item.itemName = input.next(); // read a line of text
if ( item.itemName.e qualsIgnoreCase ( "stop" ) ) // to end the while
break;
System.out.prin tln( "Enter the item number: "); // prompt for item number
item.itemNumber = input.nextInt() ; // read item number
System.out.prin tln( "Enter the item price: "); // prompt for item price
item.itemPrice = input.nextDoubl e(); // read item price
System.out.prin tln( "Enter the item total: "); // prompt for total amount of items
item.itemTotal = input.nextDoubl e(); // read item total
// call calculateValueI nventory method
item.calculateV alueInventory() ;
// end method
System.out.prin tf( "Item name is %s\n", item.itemName); // display item name
System.out.prin tf( "Item number is %d\n", item.itemNumber ); // display item number
System.out.prin tf( "Item price is %.2f\n", item.itemPrice) ; // display item price
System.out.prin tf( "Number of items in stock: %.2f\n", item.itemTotal) ; // display item total
System.out.prin tf( "Value of inventory is %.2f\n", item.valueInven tory); // 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 2101 JosAH 11,448
Recognized Expert MVP
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.readLin e() 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!
JosAH 11,448
Recognized Expert MVP
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 System.Drawing;
using System.Drawing.Printing;
using System.IO;
|
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 PrintPage event of a
PrintDocument object) to send some formatted text to the printer. However,
if the lines are too long they run off the page rather than wrapping around.
I'm sure I can spend the time and come up with a word wrapping algorithm but...
|
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. The print
method creates an instance of the PrintDocument_PrintPage method, which
I got from the following website:
...
| |
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 specify things like the size of the margins and formats for titles, paragraphs, references, page numbers, etc. Suppose that a program is to produce a report conforming to a standard. It would be desirable to provide utility class to automatically conform...
|
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 contains only 4 or 5 lines... after printing the 4 or 5 lines the should stop there ,paper shold not roll to A4 size with blank lines?
suppose a bill contains 10 lines but report designed to A4 size after printing the 10 lines the printer should stop...
|
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 have:
Page 1 = from line 1 to line 80
Page 2 = from line 81 to line 100
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |