473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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.Scanner;
public class testEmployee
{
public static void main(String args[])
{
Scanner input = new Scanner (System.in);
Employee emp = new Employee();

System.out.print("Enter hourly wage: ");
double wage = input.nextDouble();
emp.setWage(wage);

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

System.out.print("Enter hours worked: ");
double hours = input.nextDouble();

emp.calcPay();
System.out.println();

String exit = "X" ;
while (!(yourName.equals (exit)))
{
System.out.println(yourName);
System.out.println("Pay: " + emp.getPay());
System.out.println("Enter new name(X to quit)");
yourName = input.nextLine();
System.out.print("Enter new hours worked ");
hours = input.nextDouble();
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 1269
Nepomuk
3,112 Expert 2GB
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 Expert 8TB
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
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...
16
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:...
7
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...
16
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...
1
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...
3
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...
3
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...
1
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...
0
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...
0
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...

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.