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 2 of 2

Thread: sentinal not working

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sentinal not working

    can anyone tell me why the "stop" command does not work?

    package payroll;

    import java.util.Scanner;

    public class Payroll {

    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );
    String name;
    double wage;
    int hours;
    double pay;


    System.out.print( "Welcome to the Payroll Calculator\n\n" );

    System.out.print( "Enter the employee's name: " );
    name = input.nextLine();

    while (name.compareTo("stop") != 0 )
    {

    System.out.print( "Enter the employee's wage: " );
    wage = input.nextDouble();
    System.out.print( "Enter the number of hours worked: " );
    hours = input.nextInt();
    pay = wage * hours;


    System.out.printf( "\n\nName: %s\nWage: $%.2f\nHours worked: %d\nPay: $%.2f\n", name, wage, hours, pay );

    System.out.print( "Enter the employee's name: " );
    name = input.nextLine();
    }
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: sentinal not working

    It's customary in cases like this to ask: what does "does not work" mean?

    It's not that I mean to be smart or deliberately obtuse, but you are trying to hunt down a bug and you'll need to be sharp. Precisely describing the problem is one way to sharpen your mind.

    Anyway you can get a better idea of what is going on (and be able to better describe it) by adding some debugging output so you can see what the computer sees:

    while (name.compareTo("stop") != 0 )
    {
        System.out.print( "Enter the employee's wage: " ); 
        wage = input.nextDouble(); 
        System.out.print( "Enter the number of hours worked: " ); 
        hours = input.nextInt(); 
        pay = wage * hours; 
     
        System.out.printf( "\n\nName: %s\nWage: $%.2f\nHours worked: %d\nPay: $%.2f\n", name, wage, hours, pay );
     
        System.out.print( "Enter the employee's name: " ); 
        name = input.nextLine();
        System.out.println("The name read from the input stream was -->" + name + "<--"); 
    }
    Last edited by pbrockway2; February 24th, 2012 at 10:52 PM. Reason: typos

Similar Threads

  1. Jar not WORKING FOR ANYONE BUT ME!!!!!!!! HELP
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 18th, 2012, 07:32 AM
  2. not sure why the 'else is not working
    By reddevilggg in forum Loops & Control Statements
    Replies: 3
    Last Post: September 30th, 2011, 03:54 PM
  3. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM