Need help with java programming course please.?

Question by Ogre455: Need help with java programming course please.?
This is a payroll program for my class. Not looking for a handout, just some advice or a point in the right direction.

If you are experienced at Java,you will find very quickly that I am not. This is my third week of class.

This code is aimed at calculating hours worked and rate of pay. All aspects of the code seem to work fine, up until it is meant to calculate and display the results. If I use 40 hours worked and lets say 5.50 for rate, i get this

Exception in thread “main” java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:…
at java.util.Scanner.next(Scanner.java:1461…
at java.util.Scanner.nextDouble(Scanner.jav…
at payroll2.payroll2.main(payroll2.java:45)
Java Result: 1

If I don.t use a decimal for rate, it gives no error and says “build succesful” but still does not display the results.

I tried finding out what it meant but to no avail.

Also getting a parser error at the end.

Anyways here is my code, hope it is legible enough for ya. Any help is appreciated, thank you

package payroll2;

/**
*
* @author Andrew
*/
import java.util.Scanner;

public class payroll2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

System.out.println( “Welcome to the PayRoll Program” );//Introduction

Scanner input = new Scanner ( System.in );

String empName;// Declarations
double hours;
double rate;
double weeklyPay;

boolean stop = false;
{
System.out.println( “Please enter Employee’s Name or type stop to exit program:” );
empName = input.nextLine();//prompt for name

if ( empName.equals(“stop”)) {
System.out.println( “Program Exited” );
stop = true;//ends if “stop” is typed
} else {

System.out.println( “Please enter the total hours worked:” );
hours= input.nextDouble();//prompt for total hours worked

while (hours <= 0){ System.out.println( "Please enter a positive value:" ); hours= input.nextDouble();//runs if hours value was 0 or less } System.out.println( "Please enter the rate of pay:" ); rate= input.nextDouble();//prompt for hourly rate while (rate <= 0) { System.out.println( "Please Enter a Positive value:" ); rate=input.nextDouble();//runs if rate value was 0 or less //HERE is where the problem seems to be happening, of course I may be wrong weeklyPay = (double) hours * rate;//supposed to display calculate and display results System.out.printf( "Employee Name: %s, \n Weekly Pay:$ %d", empName, weeklyPay); }//parsing error at this point! Thank you very much. Compared our two and was able to fix it. Feel like I learned a few things as well. Appreciate the help. Sorry for deleting the earlier post. also =/ Best answer:

Answer by angel.white
Please don’t delete your questions, if I spend time working on it, it is really frustrating when I go to post, and it isn’t there.

I am not going to see if your new code is different than your old code (mostly because of Yahoo’s removing of white space formatting), but here is what I did with your old code: http://pastebin.org/6378

For everything I changed, I put a comment like this:
//NOTE: I did this or would do this or whatever

Have a read through the changes, I think you’re doing pretty well, and seeing what things I noticed will probably help clear up the little things here and there that could use clarity.

What do you think? Answer below!

You can leave a response, or trackback from your own site.

One Response to “Need help with java programming course please.?”

  1. Mark aka jack573 says:

    OK, angel.white did a great job.

    One thing I did notice was that you probably want the program to run more than one time. That is why you have
    boolean stop = false;
    {
    System.out.println( “Please enter Employee’s Name or type stop to exit

    So you may want to put the
    boolean stop = false;
    {
    back in.

    But, instead of the
    {
    have
    while (! stop) {

    You will want to place a closing bracket for this while loop, and I recommend it goes after the line
    System.out.printf( “Employee Name: %s, n Weekly Pay:$ %d”, empName, weeklyPay);

    So all of that code is inside the while loop. The only code outside of the while loop would be the first few lines:
    System.out.println( “Welcome to the PayRoll Program” );//Introduction

    Scanner input = new Scanner ( System.in );

    String empName;// Declarations
    double hours;
    double rate;
    double weeklyPay;

    You will also need the
    stop = true;//ends if “stop” is typed
    in the part where you check if the user typed ‘stop’.

    Hope that helps a bit.

Leave a Reply

You must be logged in to post a comment.

Powered by Yahoo! Answers