954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Ending a While loop using stop

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

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 
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.

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

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");

}
}

}

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

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.

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
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.

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

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 valuegreater 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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

oh, ok. that makes more sense

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

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.

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You