I am doing my homework and I have to create a payroll program that will loop until the user enters "stop" as the the employee's name. I also have to make the program say that only positive numbers are accepted in the event of the user entering a negative number.

I got the program to run and loop with the while loop, but I am having problems with the part the will not allow negative numbers.

I am not sure if I am suppose to use a for or an if then

Can someone please help???

I put in
if (stringHoursWorked.equals ("<0"))
System.out.printIn("positive numbers only");

and I am getting an error message that reads:
cannot find symbol
symbol : method printIn(java.lang.String)
location : class java.io.PrintStream
System.out.printIn("positive numbers only");
1 error

It is better to use an If statement for this.

EDIT: What jwenting said, I got called from my desk.

Also, the strString.equals(objObject) method compares strString to objObject.
In your case, it would compare stringHoursWorked to the string"<0". That would return true if (and only if) someone entered "<0" as the number of hours worked, it wouldn't test to see if it was a positive number.

What you need to do is to convert the string to a float or double data type. There are methods to perform this in the Float and Double classes (the contructors for those classes can also do this). Then you test that float/number data in your If statement's conditional.

Okay, I have downloaded the API, but I do not know how to really read this.

This is my third week in this Java Programming class and my instructor is not very helpful.

I don't know how to get the classes and implement them into my programs.

here is what I have for right now, and it honors the "while loop" , but I am not sure how to write the if then statement in java.

import java.util.Scanner;//program uses class Scanner
import java.io.*;
public class WeeklyPayRateProgram
{
    //main method begins execution of Java Application

    public static void main( String args [] )

    {

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

           //Variables
            String employeeName = "";
            String stringHoursWorked = "";
            String stop;

         int hoursWorked;
         int hourlyrate;
            int weeklyrate;

 [B]   if (stringHoursWorked.equals ("<0"))
        System.out.printIn("positive numbers only");[/B]

while(!employeeName.equals("stop"))
    {


    System.out.printf("\n%s\n\n%s\n", "EMPLOYEE WEEKLY PAY RATE",
                     "Enter employee name:"); //prompt
    employeeName = input.next();

    //prompt the user to enter the Hours Worked

    System.out.printf(" Enter hourly rate:"); //prompt
    hourlyrate  = input.nextInt();
    System.out.printf(" Enter hours worked:"); //prompt
    hoursWorked = input.nextInt();

    weeklyrate = hoursWorked * hourlyrate; //multiply numbers
    System.out.printf( "weeklyrate is $%d", weeklyrate);//display weekly rate

             System.out.println("\n The name entered was: " + employeeName);
                System.out.printf("\n The Weekly rate entered was:" + "$%d",
                                            weeklyrate );
    } // end while

    }
}//end

Edited 3 Years Ago by happygeek: fixed formatting

Read carefully, that's not a capital I but a lowercase l.

I must don't have a good eye, because I cannot find what you are talking about.

I am still looking though, I am determined to get this programming thing down!!

I want an A in this class.

The line:

System.out.printIn("positive numbers only");

Is wrong. It should be;

System.out.println("positive numbers only");

Note that the method is println(), not printIn(). That's a lowercase "ell" (l for Lima), not an uppercase "eye" (i for India).
That's why it won't compile.

And again, I say. Your If conditional is wrong. It is returning True when the String stringHoursWorked is equal to the String '<0'. What you want is a conditional which will return True if a number is less than zero.

Use the intHoursWorked as your conditional variable not stringHoursWorked.

okay, I changed the "I" to a lower case "i",

I also change my if condition to

if (inthoursWorked.equals("<0"))
else System.out.printin("positive numbers only");

and I am getting an error message that says "else" without "if"
else System.out.printin("positive numbers only");
1 error

No, not an i, an l (lemon, lima, lentils etc. The letter between k and m)

Your If statement is in the wrong place, it should be here:

System.out.println(" Enter hours worked:"); //prompt
hoursWorked = input.nextInt();
if (hoursWorked < 0){
//statements;
}

Your statements should not only tell the user that they input an incorrect number, but it should also prompt for a correct number, and have an internal loop to keep prompting until a positive number has been given.

Also, leave this line as a printf() call:

System.out.printf("\n%s\n\n%s\n", "EMPLOYEE WEEKLY PAY RATE","Enter employee name:");

But change all the other printf() calls to println() calls. As it stands these prompts are not being displayed until an error occurs.

And please indent your code, or if you do use [code] type the code here [/code] tags when you post more than a line.

BTW, how are you compiling the code. Writted as a text file then sent through a command line compiler? Or are you using an IDE such as NetBeans?

This article has been dead for over six months. Start a new discussion instead.