Results 1 to 10 of 10
  1. #1
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default 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

  2. #2
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Can you post your code please ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    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

  5. #5
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    You have a very simple error ^_^

    please change this:
    Java Code:
    while ( !employeeName.equalsIgnoreCase ("stop") );
    to This:
    Java Code:
    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.

  6. #6
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  8. #8
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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.

  9. #9
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    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!

  10. #10
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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.

Similar Threads

  1. How to stop entering Data
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 02:58 AM
  2. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 06:21 AM
  3. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 02:13 AM
  4. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 02:44 PM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-09-2007, 11:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •