Is someone willing to help me with this problem? I am new to java programming, have read lots of material but have not been able to put it together. Any help will be much appreciated. thanks,

Sandawg

Recommended Answers

All 12 Replies

it's not stop, it's break and this is the sun tutorial on it

Thanks for your response. I used the the word 'stop' because the assignment was/is to have the loop end when the word 'stop' is entered.

oh, ok. that makes a bit more sense, did you figure it out?

if not your current code would be very helpful to figure out what you need to do.

oh, ok. that makes a bit more sense, did you figure it out?

if not your current code would be very helpful to figure out what you need to do.

Thanks, keep in mind, I have no idea or understanding of java programming, so this is what I have:

import java.util.Scanner;// used to read keyboard input for integers
import java.io.*; // used to read keyboard input for characters

class PayrollCalculator
{
   public static void main (String arg []) throws IOException

   {
    Scanner input = new Scanner (System.in);

    String employeeName;
    //String stop;



    //TextIO.put ("Enter employee's name:");

    //employeeName = TextIO.getln();

    System.out.print ("Enter Employee's name or type stop to quit program:");

    employeeName = input.next();


    while (employeeName != "stop" )
    {


    System.out.print ("Enter employee's hourly pay rate:");
    double payrate = input.nextDouble();// used to input real numbers


    while (payrate <= -1)
    {
    System.out.print ("Please enter a positive number.");
    payrate = input.nextDouble();
    }

    System.out.print ("Enter number of hours worked:");
    double hours = input.nextDouble();
    System.out.println ();// used for white space


    while (hours <= -1)
    {
    System.out.print ("Please enter a positive number.");
    hours = input.nextDouble();
    }


    double salary = payrate * hours;// pay calculation


    System.out.print (employeeName );
    System.out.print (" - your salary for the week is: ");
    System.out.printf( "$%.2f\n", salary);// used to format currency output
    System.out.println ();// used for white space

    System.out.print ("Enter Employee's name or type stop to quit program:");
    employeeName = input.next();

    }
    if (employeeName == "stop")
{
    System.out.println ("Thank you, come again");

    }
   }

}

please use code tags so your code looks like this:
(NOTE: i haven't done anything to your code.)

import java.util.Scanner ;// used to read keyboard input for integers
import java.io.* ; // used to read keyboard input for characters

class PayrollCalculator {
	public static void main ( String arg[] ) throws IOException {
		Scanner input = new Scanner ( System.in ) ;
		String employeeName ;
		//String stop;
		//TextIO.put ("Enter employee's name:");
		//employeeName = TextIO.getln();
		System.out
				.print ( "Enter Employee's name or type stop to quit program:" ) ;
		employeeName = input.next ( ) ;
		while ( employeeName != "stop" ) {
			System.out.print ( "Enter employee's hourly pay rate:" ) ;
			double payrate = input.nextDouble ( ) ;// used to input real numbers
			while ( payrate <= - 1 ) {
				System.out.print ( "Please enter a positive number." ) ;
				payrate = input.nextDouble ( ) ;
			}
			System.out.print ( "Enter number of hours worked:" ) ;
			double hours = input.nextDouble ( ) ;
			System.out.println ( ) ;// used for white space
			while ( hours <= - 1 ) {
				System.out.print ( "Please enter a positive number." ) ;
				hours = input.nextDouble ( ) ;
			}
			double salary = payrate * hours ;// pay calculation
			System.out.print ( employeeName ) ;
			System.out.print ( " - your salary for the week is: " ) ;
			System.out.printf ( "$%.2f\n" , salary ) ;// used to format currency output
			System.out.println ( ) ;// used for white space
			System.out
					.print ( "Enter Employee's name or type stop to quit program:" ) ;
			employeeName = input.next ( ) ;
		}
		if ( employeeName == "stop" ) {
			System.out.println ( "Thank you, come again" ) ;
		}
	}
}

now, i do see a few things,
1. employeeName!="stop" will not work in the expected way, instead you should use !employeeName.equals("stop") will act as expected it will be case sensitive. the same is true for "==", it won't work either.
2. why are you using a loop to get input for one wage (like payrate and hours) ? i don't think you need the loops.

please use code tags so your code looks like this:
(NOTE: i haven't done anything to your code.)

import java.util.Scanner ;// used to read keyboard input for integers
import java.io.* ; // used to read keyboard input for characters

class PayrollCalculator {
	public static void main ( String arg[] ) throws IOException {
		Scanner input = new Scanner ( System.in ) ;
		String employeeName ;
		//String stop;
		//TextIO.put ("Enter employee's name:");
		//employeeName = TextIO.getln();
		System.out
				.print ( "Enter Employee's name or type stop to quit program:" ) ;
		employeeName = input.next ( ) ;
		while ( employeeName != "stop" ) {
			System.out.print ( "Enter employee's hourly pay rate:" ) ;
			double payrate = input.nextDouble ( ) ;// used to input real numbers
			while ( payrate <= - 1 ) {
				System.out.print ( "Please enter a positive number." ) ;
				payrate = input.nextDouble ( ) ;
			}
			System.out.print ( "Enter number of hours worked:" ) ;
			double hours = input.nextDouble ( ) ;
			System.out.println ( ) ;// used for white space
			while ( hours <= - 1 ) {
				System.out.print ( "Please enter a positive number." ) ;
				hours = input.nextDouble ( ) ;
			}
			double salary = payrate * hours ;// pay calculation
			System.out.print ( employeeName ) ;
			System.out.print ( " - your salary for the week is: " ) ;
			System.out.printf ( "$%.2f\n" , salary ) ;// used to format currency output
			System.out.println ( ) ;// used for white space
			System.out
					.print ( "Enter Employee's name or type stop to quit program:" ) ;
			employeeName = input.next ( ) ;
		}
		if ( employeeName == "stop" ) {
			System.out.println ( "Thank you, come again" ) ;
		}
	}
}

now, i do see a few things,
1. employeeName!="stop" will not work in the expected way, instead you should use !employeeName.equals("stop") will act as expected it will be case sensitive. the same is true for "==", it won't work either.
2. why are you using a loop to get input for one wage (like payrate and hours) ? i don't think you need the loops.

Thanks, the reason for the loop is to continue to enter employee names until stop is entered and thus exit the loop.

why do you perform this if:

if ( employeeName == "stop" ) {
System.out.println ( "Thank you, come again" ) ;
}

we allready know the employeeName at that point is "stop", otherwise you would still be in the loop

Thanks, the reason for the loop is to continue to enter employee names until stop is entered and thus exit the loop.

not the main loop, i get why you are using a loop there but why this:

while ( payrate <= - 1 ) {
    System.out.print ( "Please enter a positive number." ) ;
    payrate = input.nextDouble ( ) ;
}

all this does is keep taking in a number and rewriting a variable's value until the value is less than -1. you do this twice, and there is no need for the while loop.

not the main loop, i get why you are using a loop there but why this:

while ( payrate <= - 1 ) {
    System.out.print ( "Please enter a positive number." ) ;
    payrate = input.nextDouble ( ) ;
}

all this does is keep taking in a number and rewriting a variable's value until the value is less than -1. you do this twice, and there is no need for the while loop.

I believe you are mis-reading the loop. It will loop until a value greater than -1 is entered, the intent being to ensure a positive value for the rate. It really should use payrate <= 0 though, since anything less than or equal to zero would be invalid.

To the poster, sciwizeh already pointed out the "stop" loop problem. Use !employeeName.equals("stop") or !employeeName.equalsIgnoreCase("stop") to compare the strings. Do not use == or != for string comparison.

oh, ok. that makes more sense

why do you perform this if:

if ( employeeName == "stop" ) {
System.out.println ( "Thank you, come again" ) ;
}

we allready know the employeeName at that point is "stop", otherwise you would still be in the loop

Thanks for your response. As previously mentioned, I don't know what I am doing and trying to learn.

not the main loop, i get why you are using a loop there but why this:

while ( payrate <= - 1 ) {
    System.out.print ( "Please enter a positive number." ) ;
    payrate = input.nextDouble ( ) ;
}

all this does is keep taking in a number and rewriting a variable's value until the value is less than -1. you do this twice, and there is no need for the while loop.

Another requirement of the problem was/is to require entry of positive numbers only. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.