473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need urgent help with program. displays 2 lines instead of 1

23 New Member
hi i created this program for a university project but i don't know how to fix the problem. here is the program:

// class
public class Employee
{
private double Wage = 0, pay = 0;
private double hoursWork;

public void setWage(double aWage)
{
Wage = aWage;
}
public void calcPay()
{
if (hoursWork <= 40)
{
pay = hoursWork * Wage;
}
if ( hoursWork > 40)
{
pay = (40 * Wage) + ((hoursWork - 40) * (1.5 * Wage));
}
}
public double getPay()
{
return pay;
}
}


// testClass
import java.util.Scann er;
public class testEmployee
{
public static void main(String args[])
{
Scanner input = new Scanner (System.in);
Employee emp = new Employee();

System.out.prin t("Enter hourly wage: ");
double wage = input.nextDoubl e();
emp.setWage(wag e);

System.out.prin t("enter the employee name(X to quit): " );
String yourName = input.nextLine( );

System.out.prin t("Enter hours worked: ");
double hours = input.nextDoubl e();

emp.calcPay();
System.out.prin tln();

String exit = "X" ;
while (!(yourName.equ als (exit)))
{
System.out.prin tln(yourName);
System.out.prin tln("Pay: " + emp.getPay());
System.out.prin tln("Enter new name(X to quit)");
yourName = input.nextLine( );
System.out.prin t("Enter new hours worked ");
hours = input.nextDoubl e();
emp.calcPay();
}
}
}


the output i get is:
Enter hourly wage: 100
enter the employee name(X to quit): Enter hours worked:

the program doesn't allow me to enter the name. it just prints the hours worked right next to it
Aug 23 '07 #1
2 1289
Nepomuk
3,112 Recognized Expert Specialist
You must wait for the next Line to come:
Expand|Select|Wrap|Line Numbers
  1. // testClass
  2. import java.util.Scanner;
  3. public class testEmployee
  4. {
  5.   public static void main(String args[])
  6.   {
  7.     Scanner input = new Scanner (System.in);
  8.     Employee emp = new Employee();
  9.  
  10.     System.out.print("Enter hourly wage: ");
  11.     double wage = input.nextDouble();
  12.     emp.setWage(wage);
  13.  
  14.     System.out.print("enter the employee name(X to quit): " );
  15.     input.nextLine();
  16.     String yourName = input.nextLine();
  17.  
  18.     System.out.print("Enter hours worked: ");
  19.     double hours = input.nextDouble();
  20.  
  21.     emp.calcPay();
  22.     System.out.println();
  23.  
  24.     String exit = "X" ;
  25.     while (!(yourName.equals (exit)))
  26.     {
  27.       System.out.println(yourName);
  28.       System.out.println("Pay: " + emp.getPay());
  29.       System.out.println("Enter new name(X to quit)");
  30.       input.nextLine();
  31.       yourName = input.nextLine();
  32.       System.out.print("Enter new hours worked ");
  33.       hours = input.nextDouble();
  34.       emp.calcPay();
  35.     }
  36.   }
  37. }
  38.  
(That's just 2 "input.nextLine ();"s added before you define "yourName". )
Aug 23 '07 #2
JosAH
11,448 Recognized Expert MVP
When you enter the hourly wages you type something like "123.45<enter>" .
The nextDouble() method returns 123.45 and it leaves the <enter> character in
the input buffer. When you want to read the name, the scanner sees an 'empty
line' in the input buffer and continues reading the next item.

In short: you should get rid of that <enter> character in the input stream. Simply
invoke readLine() to get rid of that dummy (empty) line before you want to read
the name.

kind regards,

Jos
Aug 23 '07 #3

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

Similar topics

3
10668
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
16
2968
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In: http://www.mywebsite.org/WebResource.axd?d=5WvLfhnJp5Lc8WhQSD4gdA2&t=632614619884218750 -------------------------------------------------------------------------------- System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at...
7
2185
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the "continue_function", and if NO is returned to "get_data", display_report executes and the program ends? Basically I am having trouble understanding the program flow within the "if" loop in the "main" function.
16
2540
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
1
5689
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for frmRandom, i tried it with the Sleep API and a Do/Until Loop and neither worked, could you please help me, I have my code below. frmMachine 'Slot Machine, (c) 2006 Peter Browne, GPL Licensed ' 'Peter Browne 'Sheridan Corporate Centre '2155 Leanne...
3
5616
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we want to give them the opportunity to run these modules from home as well, we are trying to get the USERNAME/COMPUTERNAME as well, so the students, when they sign up for the modules, they can ONLY run the modules fromhome PC. We found a nice...
3
2106
trkrbabe
by: trkrbabe | last post by:
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. ...
1
2106
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
4
3897
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it and did see the problem on my side. There're three problems in the source code of your project. 1. You should move the following lines of code from the GetGeometry method within the CustomPolyLine class: _points.Add(new Point(10, 10));
0
9647
marktang
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...
0
9496
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,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6745
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();...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.