|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-09-2008, 06:02 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 22
|
|
|
Entering a while loop with a not equal to string
I am trying to enter a while loop when a string is not equal to "stop". However, I cannot seem to get it work correctly. I have tried using:
while (!variable.equalsIgnoreCase ("stop") )
and it does not work. I have also tried:
while variable != "stop" // even though != is not a string operator.
I either end up entering the loop when I enter "stop" or I end up with an infinite loop - which really stinks. I have Googled my brains out and can't find anything on entering a while loop if a string is not equal to another string. I have found information for if...else statements, but I am not allowed to use if...else statements. I have to use a while statement.
If anyone could help I would very, very appreciative!
Brian
|
|

07-09-2008, 06:08 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
|
Can you post your code please ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

07-09-2008, 06:11 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
|
Did you try that is equal. If so check the value of variable
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-09-2008, 06:22 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 22
|
|
|
Sure. Here is the entire code. It is a simple payroll program that uses Scanner to input employee name (the variable in question), hourly rate and hours worked and displays the employee's name and gross pay. No overtime calculations are required. I want to exit the program when stop is entered and display an exit message. Here is the code:
// Payroll Program by Brian M. King
// This program calculates an employee's weekly pay
// Week 3 Assignment IT 215
import java.util.Scanner;
public class Payroll2
{
public static void main( String args [] )
{
String employeeName; // Employee's name
double hours; // number of hours worked
double hourlyRate; // employee's hourly rate of pay
double weeklyPay; //calculation of employee's weekly pay
//create scanner to obtain input from command window
Scanner input = new Scanner( System.in );
System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
employeeName = input.next (); // read employee name
while ( !employeeName.equalsIgnoreCase ("stop") );
{
System.out.print( "Enter the number of hours the employee worked: " ); // prompt
hours = input.nextDouble (); // read number of hours
while ( hours < 0 ) // verify a positive value for hours worked
{
System.out.print ( "Please enter a positive value for hours worked:" );// error message
hours = input.nextDouble (); // new value for hours
}
System.out.print( "Enter the employee's hourly rate of pay: $" ); // prompt
hourlyRate = input.nextDouble (); // read employee rate of pay
while (hourlyRate < 0 )
{
System.out.print( "Please enter a positive value for hourly rate of pay:" );// error message
hourlyRate = input.nextDouble ();// new value for hourly rate
}
weeklyPay = hours * hourlyRate; // calculate weekly pay
System.out.printf( "Employee %s", employeeName ); // display employee name
System.out.printf( " earned $%.2f\n", weeklyPay ); // display weekly pay
}
System.out.println( "Thank you for using the Payroll Program!" ); // exit message
} // end method main
} // end class Payroll2
|
|

07-09-2008, 06:27 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
You have a very simple error ^_^
please change this:
while ( !employeeName.equalsIgnoreCase ("stop") );
to This:
while ( !employeeName.equalsIgnoreCase ("stop") )
You put a ";". ^_^ Try removing it ^_^
I hope that helps
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

07-09-2008, 06:48 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
|
Just found a bug on your code.
If i entered a name, it will loop infinitely asking for rate nad number of hours for that same employee.
Try fixing that one ^_^ That is another logic error in your code.
Clue: It has something to do with the line the inputs the employee name and ofcourse the while loop ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

07-09-2008, 06:50 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
|
Additional information. Such an error should pointed in a Java IDE. Not exactly you get the error explanation there. But just looking at the error message you should have an idea what happened. Isn't it Eku?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-09-2008, 06:58 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
|
yup the error gives a lot of Clue, but in this case there is no error messages. it is purely Logical error in his loop. ^_^ He can figure that one. =P His Loops going infinitely if you entered a name. ^^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

07-09-2008, 07:15 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 22
|
|
|
Thanks so much for your help - with the semi-colon and not prompting for a name again! I really thought I was going to pull my hair out!
|
|

07-09-2008, 08:10 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
|
No Problem ^_^ just try fixing that bug and ask again if your stuck at something ^^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|