Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Program not recognizing string sentinel

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Program not recognizing string sentinel

    Hello,

    I'm new to Java and am trying to create a simple payroll program that uses a while loop to repeat until the user enters "Stop" for the employee's name. I've tried everything I can think of and everything others have suggested and for some reason, I cannot get the program to differentiate between the word "Stop" or any other word. Could someone please help me figure out what I'm doing wrong? My code is as follows:

    import java.util.Scanner; // imports scanner class for user input

    public class Payrolltest
    {
    public static void main( String[] args ) // begins execution of program
    {
    Scanner input = new Scanner( System.in ); // creates a scanner to obtain user input

    String Employee = "name"; // employee's name
    double Hours = 0; // hours worked
    double Rate = 0; // employee's pay rate
    double Paycheck = 0; // amount of the paycheck

    System.out.println(); // blank line for readability



    while (!Employee.equals("Stop")) //begins the repetition loop for employee input until user enters the sentinel stop.
    {
    System.out.println ("Please enter the employee's name. Enter \"Stop\" to quit."); //prompts for the employee's name.

    Employee = input.nextLine(); // read input from user and assign to Employee
    System.out.println ("Enter hours worked:"); // prompt for hours worked
    Hours = input.nextDouble(); // read input from user and assign to Hours

    System.out.println ("Enter employee's hourly pay rate:"); // prompt for pay rate
    Rate = input.nextDouble(); // read input from user and assign to Rate

    Paycheck = Hours * Rate; // calculates amount for paycheck

    System.out.println(); // blank line for readability

    System.out.printf ("%s's paycheck is $%,.2f", Employee, Paycheck); // displays employee's name and paycheck amount

    System.out.println(); // blank line for readability
    System.out.printf ("Do you want to continue? enter the next employee's name or enter \"Stop\" to quit.)");
    Employee = input.nextLine(); // read input from user and assign to Employee

    } // end repetition loop for user input

    System.out.printf ("Thank you for using the payroll program."); // goodbye message

    } // end method main
    } // end class Payroll

    Thanks!


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Program not recognizing string sentinel

    Just a side note first:
    When your comparing two string is Java, remember you use the assignment operator as
    Strings are objects themselves - so you compare them to each other. Not the equality
    operator '=='.

    If you are choosing a sentinel controlled loop - you generally have to ask the first value
    input before you enter the body of the controlling loop. For example:

    public class TestMe
    {
       public static void main(String[] args)
       {
          double x = 0.0f;
     
          System.out.print("Enter a value, enter -1 to quit: ")
          x = input.nextDouble();
     
          // ok - we have the first value of 'x' - so let's test against the continuation 
          // condition shall we
          while (x != -1)
          {
             double total += x;
             // ask for the next input here - breaking if input matches sentinel condition
          } // if the test is false - the entire loop is skipped
           // and control moves to any executable statements
       }
    }

    Generally, it is also better to not use a string data type when comparing a sentinel value.
    If you want to use a String, use a single character like "x" for example as the terminating
    condition.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (June 13th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not recognizing string sentinel

    Hi Ada,

    Thanks for the advice. I've been told from several others that == cannot be used to compare strings and even tried that in desperation with no luck. I have used single characters and integers as sentinels in the past with success, however one of the requirements of this program is that the user needs to be able to enter the word stop instead of an integer or single character. Is there a work around that I can use? I've tried everything I can think of and don't know where to go from here.

    Thanks again for your help,

    BethM

  5. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Program not recognizing string sentinel

    Have you considered other built in String methods?

    matches() combined with the negation operator ! in a continuation
    condition will terminate a sentinel controlled loop when the entered
    string matches the string placed within the parentheses.

    Hope this helps.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Program not recognizing string sentinel

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Welcome to the forum. Please post your updated code correctly.
    one of the requirements of this program is that the user needs to be able to enter the word stop instead of an integer or single character. Is there a work around that I can use?
    No, there's no workaround to avoid the program's requirements. Your while() loop is poorly constructed to accomplish what you're trying to do. Asking for the sentinel value at the beginning of the loop will not prevent the remainder of the loop from executing unless an 'if' statement is added to break out of the loop at that point, and I'm not recommending it, as it's not necessary.

    Rethink your loop construction and where in the loop the sentinel value is acquired.

Similar Threads

  1. [SOLVED] Beginner // switch not recognizing upper case Q
    By Christian_Doucet in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 2nd, 2014, 10:02 PM
  2. Trying to use CTRL-D to terminate the program (sentinel value)
    By loui345 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2014, 08:13 PM
  3. [SOLVED] Need help with Sentinel Control Loop
    By Roku in forum Loops & Control Statements
    Replies: 1
    Last Post: March 21st, 2013, 06:52 AM
  4. Eclipse not recognizing my method for class.
    By jerryg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2012, 06:52 PM
  5. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM