Results 1 to 10 of 10
- 07-09-2008, 05:02 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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, 05:08 AM #2
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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, 05:11 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try that is equal. If so check the value of variable
- 07-09-2008, 05:22 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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, 05:27 AM #5
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
You have a very simple error ^_^
please change this:
to This:Java Code:while ( !employeeName.equalsIgnoreCase ("stop") );
You put a ";". ^_^ Try removing it ^_^Java Code:while ( !employeeName.equalsIgnoreCase ("stop") )
I hope that helpsMind only knows what lies near the heart, it alone sees the depth of the soul.
- 07-09-2008, 05:48 AM #6
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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, 05:50 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 07-09-2008, 05:58 AM #8
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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, 06:15 AM #9
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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, 07:10 AM #10
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Similar Threads
-
How to stop entering Data
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 02:58 AM -
checking if there are equal numbers
By nalinda in forum New To JavaReplies: 1Last Post: 11-18-2007, 06:21 AM -
checking if there are equal numbers
By nalinda in forum New To JavaReplies: 0Last Post: 11-18-2007, 02:13 AM -
i need assistance with a string triggered loop please!
By Phobos0001 in forum New To JavaReplies: 9Last Post: 11-14-2007, 02:44 PM -
terminating a while loop with a string
By tkdvipers in forum New To JavaReplies: 3Last Post: 07-09-2007, 11:23 PM


LinkBack URL
About LinkBacks

Bookmarks