Help with my code.

I can't figure out why it will not calculate any real numbers (like decimals). Thanks!

// Payroll.java // file name of program

// Payroll Program //name of program

import java.util.Scanner;//imports the scanner program to be used to track user inputs

public class Payroll //defines the class

{

public static void main( String args[])

{

Scanner input = new Scanner(System.in);

float hlyPay;

float hrsWrkd;

float pay;

System.out.print("Payroll Calculation Program"); //prints line to describe program

System.out.println();//adds an extra line to space header away from body

System.out.println();//adds an extra line to space header away from body

System.out.print("Enter Employee Name:");

String empName = input.nextLine();//allows program to capture information entered by user for later use

System.out.print("Enter Hourly Rate of Employee:$");

hlyPay = input.nextFloat();//allows program to capture information about the hourly rate of the employee

System.out.print("Enter Number of Hours Employee Worked:");

hrsWrkd = input.nextFloat();//allows program to capture information about the hours worked for the employee

pay = hlyPay * hrsWrkd;//finds the pay based on the hourly rate and hours worked from the employee

System.out.printf("%s's weekly pay amount is $%d.",empName,pay);//prints employees name and the pay the employee will receive for the week

}

}

[1511 byte] By [hurstta] at [2007-11-27 5:58:28]
# 1
> hlyPay = input.nextFloat();What consumes the newline before the next input? Somewhere you probably need to invoke input.nextLine() to eat it.And please get rid of just about all those totally redundant and noisy comments in the code.
warnerjaa at 2007-7-12 16:33:09 > top of Java-index,Java Essentials,Java Programming...
# 2

warnerja,

Thank you for your help. I actually solved it by adding %.2f in the last print line so it would accept the decimals. I leave the comments in there because this is for a class and I want the teacher to know I understand each line.

Maybe you can help with this next issue. I need to make it so it continues to request employee information until the user enters stop as the employee name.

This is what I have(no comments included):

import java.util.Scanner;

public class Payroll2

{

public static void main( String args[])

{

Scanner input = new Scanner(System.in);

float hlyPay;

float hrsWrkd;

float pay;

System.out.println();

System.out.print("Payroll Calculation Program");

System.out.println();

System.out.println();

System.out.print("Enter Employee Name:");

String empName = input.nextLine();

System.out.printf("Enter the hourly rate for %s:$",empName);

hlyPay = input.nextFloat();

while (hlyPay < 0.00)

{

System.out.print("Please re-enter the hourly rate using only positive numbers:$");

hlyPay = input.nextFloat();

}

System.out.printf("Enter number of hours %s worked:",empName);

hrsWrkd = input.nextFloat();

while (hrsWrkd < 0.00)

{

System.out.printf("Please re-enter the number of hours that %s worked using only positive numbers:",empName);

hrsWrkd = input.nextFloat();

}

pay = hlyPay * hrsWrkd;

System.out.printf("%s's weekly pay amount is $%.2f.\n",empName,pay);

System.out.println();

System.out.println();

}

}

hurstta at 2007-7-12 16:33:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Please, please use code tags.

Try this:

import java.util.Scanner;

public class Payroll2 {

public static void main( String args[]) {

Scanner input = new Scanner(System.in);

float hlyPay;

float hrsWrkd;

float pay;

System.out.println();

System.out.print("Payroll Calculation Program");

System.out.println();

System.out.println();

System.out.print("Enter Employee Name:");

String empName = input.nextLine();

while ( !empName.equalsIgnoreCase("stop") )

{

System.out.printf("Enter the hourly rate for %s:$",empName);

hlyPay = input.nextFloat();

while (hlyPay < 0.00) {

System.out.print("Please re-enter the hourly rate using only positive numbers:$");

hlyPay = input.nextFloat();

}

System.out.printf("Enter number of hours %s worked:",empName);

hrsWrkd = input.nextFloat();

while (hrsWrkd < 0.00) {

System.out.printf("Please re-enter the number of hours that %s worked using only positive numbers:",empName);

hrsWrkd = input.nextFloat();

}

pay = hlyPay * hrsWrkd;

System.out.printf("%s's weekly pay amount is $%.2f.\n",empName,pay);

System.out.println();

System.out.println();

input.nextLine(); //need to eat one extra line somewhere; never worked with Scanner before, so no idea why.

System.out.print("Enter Employee Name:");

empName = input.nextLine();

}

}

}

nogoodatcodinga at 2007-7-12 16:33:09 > top of Java-index,Java Essentials,Java Programming...