Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-29-2008, 09:49 PM   #1 (permalink)
Trotter's Avatar
 

Join Date: Jan 2005

Location: The South

Posts: 19,915

Trotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to all

Default Java problem

I'm stumped, and I know it is simple.

Here's what I need this to do:
Quote:
Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
never mind the original program... I had it right. Here is what I have right now:
Quote:
// Employee Payroll Program 2
// Acquire employee name, hourly rate, and hours worked,
// then display employee name and weekly wage

import java.util.Scanner; // program uses Scanner

public class Payroll2
{

// main method begins execution of Java application
public static void main( String args[] )
{

// Welcome message for Employee Payroll Program
System.out.println( "Welcome to the Employee Payroll Program" );

// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

// prompt for and input employee name
System.out.println( "Please enter the employee's name or " );
System.out.println( "enter \"stops\" to exit the program." );
String employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

while ( employeeName != "stops" )
{
// prompt for and input hourly rate
double hourlyRate; // hourly rate amount read from user

System.out.println( "Please enter the employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input

while ( hourlyRate < 0 )
{
System.out.println( "Hourly rate must be a positive number." ); // error message
System.out.println( "Please enter employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input

} // end while

System.out.println(); // outputs a blank line

// prompt for and input number of hours worked
double hoursWorked; // number of hours worked read from user

System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input
System.out.println(); // outputs a blank line

while ( hoursWorked < 0 )
{
System.out.println( "The number of hours worked must be a positive number." ); // error message
System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input

} // end while

// compute weekly pay
double weeklyPay; // set weekly pay as primitive variable

weeklyPay = hourlyRate * hoursWorked;

// display employee name and weekly pay
System.out.printf( "The employee's name is %s\n", employeeName );
System.out.println(); // outputs a blank line

System.out.printf( "The weekly pay is $ %.2f\n", weeklyPay );
System.out.println(); // outputs a blank line

// prompt for and input employee name
System.out.println( "Please enter the employee's name or " );
System.out.println( "enter \"stops\" to exit the program." );
employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

} // end while

} // end main

} //end class Payroll2
I originally had "String employeeName = input.nextLine(); // read a line of text" as the next to last line before the end of the main while loop, but I kept getting a compilation error because I had already declared employeeName earlier.

My problem now is that the program skips over letting the user enter anew name once it goes through the first time. It prints the prompt, but skips the input.

If anyone can help, please jump in. I am supposed to submit this in two hours.

EDIT: It delete all my white space... sorry about that.
__________________
R.I.P. Danny L. Trotter , 14 Nov 1945 - 4 Sept 2009




DFI LanParty-UT SLI-D - Windows 7 64-bit - AMD Athlon X2 4200+ w/CNPS9500
4GB RAM(4x1GB) - Razer Lachesis - EVGA GTX 260 Core 216 896MB


>>>> I am looking for donated DDR2 (link) <<<<

< < < < < If I've been helpful, rep me. . . .
Trotter is offline  
Old 02-29-2008, 10:24 PM   #2 (permalink)
Trotter's Avatar
 

Join Date: Jan 2005

Location: The South

Posts: 19,915

Trotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to all

Default Re: Java problem

Got it, thanks to Google:
Quote:
// Employee Payroll Program 2
// Acquire employee name, hourly rate, and hours worked,
// then display employee name and weekly wage

import java.util.Scanner; // program uses Scanner

public class Payroll2
{

// main method begins execution of Java application
public static void main( String args[] )
{

// Welcome message for Employee Payroll Program
System.out.println( "Welcome to the Employee Payroll Program" );

// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

String employeeName; // declare employeeName as string
double hourlyRate; // declare hourlyRate as double
double hoursWorked; // declare hoursWorked as double
double weeklyPay; // declare weeklyPay as double

do
{

// prompt for and input employee name
System.out.println( "Please enter the employee's name or " );
System.out.println( "enter \"stops\" to exit the program." );
employeeName = input.next(); // read a line of text

if ( employeeName.equals ( "stops" ) )
{break;
} // check exit loop condition

System.out.println(); // outputs a blank line

// prompt for and input hourly rate

System.out.println( "Please enter the employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input

while ( hourlyRate < 0 )
{

System.out.println( "Hourly rate must be a positive number." ); // error message
System.out.println( "Please enter employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input

} // end while

System.out.println(); // outputs a blank line

// prompt for and input number of hours worked

System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input
System.out.println(); // outputs a blank line

while ( hoursWorked < 0 )
{
System.out.println( "The number of hours worked must be a positive number." ); // error message
System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input

} // end while

// compute weekly pay

weeklyPay = hourlyRate * hoursWorked;

// display employee name and weekly pay
System.out.printf( "The employee's name is %s\n", employeeName );
System.out.println(); // outputs a blank line
System.out.printf( "The weekly pay is $ %.2f\n", weeklyPay );
System.out.println(); // outputs a blank line

} while ( true ); // end do - while

} // end main

} //end class Payroll2

__________________
R.I.P. Danny L. Trotter , 14 Nov 1945 - 4 Sept 2009




DFI LanParty-UT SLI-D - Windows 7 64-bit - AMD Athlon X2 4200+ w/CNPS9500
4GB RAM(4x1GB) - Razer Lachesis - EVGA GTX 260 Core 216 896MB


>>>> I am looking for donated DDR2 (link) <<<<

< < < < < If I've been helpful, rep me. . . .
Trotter is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with 3D graphics mordi05 Windows Operating Systems and Software 6 12-10-2007 11:37 PM
(Java) Problem creating and passing an Integer object? Toshiro Programming Discussions 1 10-17-2007 08:00 PM
Media Library (I Presume) Problem. Nadegas Windows Operating Systems and Software 4 06-08-2007 08:27 AM
Chronic Rebooting Problem AND_YOU_ARE Hardware Troubleshooting 1 04-24-2007 06:04 PM
Java problem JHC130 Browser & General Internet Questions 7 04-19-2007 06:02 PM