tags:

views:

126

answers:

4

I'm trying out several exercises from a Java programming book. I have the code below:

import java.io.*;
import java.util.Scanner;

public class Ex420
{
 public static void main( String args[] )
 {
  String employeeName = "";
  double workHours,excessHours, hourlyRates, grossPay;
  Scanner input = new Scanner( System.in );

  while ( employeeName != "stop" )
  {
   System.out.printf( "\nInput employee name or stop to exit: " );
    employeeName = input.nextLine();
   System.out.printf( "Input working hours: " );
    workHours = input.nextDouble();
   System.out.printf( "Input hourly rates: " );
    hourlyRates = input.nextDouble();

   if ( workHours <= 40 & workHours >= 0 )
   {
    excessHours = 0;
    grossPay = hourlyRates * workHours;
    System.out.printf( "%s's gross pay is $%.2f\n", employeeName, grossPay );
   }
   else if ( workHours > 40 )
   {
    excessHours = workHours - 40;
    grossPay = hourlyRates * 40 + 1.5 * hourlyRates * excessHours;
    System.out.printf( "\n%s's worked for %.1f excess hours.\n", employeeName, excessHours );
    System.out.printf( "%s's gross pay is $%.2f\n", employeeName, grossPay ); 
   }
   else
   {
    System.out.printf( "Invalid input. Please try again." );
   }  
  } // end while
 } // end main
} // end class Ex420

The problem is, the while loop doesn't seem to be working. Whenever I input "stop" as the employeeName, the program just goes on. I tried replacing "stop" with any other String and it still doesn't work. But when I try initializing employeeName with "stop", the program quits right away, which is expected. What am I doing wrong here?

Furthermore, after the first loop, the program always skips asking the employeeName. I tried replacing employeeName = input.nextLine(); with employeeName = input.next(); and it doesn't skip it anymore. I'm wondering though, is there any way I can make it not skip the input when using employeeName = input.nextLine();?

Thanks in advance for the help!

+12  A: 

When comparing strings in Java use equals method, not == or != operators. By using those operators you simply compare references to objects and not their contents. So your condition should look like

while ( !"stop".equals(employeeName) )

Note the that "stop" is first because theoretically your employeeName variable can be null. This way the code is not going to throw NullPointerException by calling equals method on the null object.

eugener
You probably mean `while (!"stop".equals(employeeName))` to match the meaning of the example code, right?
maerics
I like the 'Yoda' expression :)
VoodooChild
+2  A: 

The problem is you're not using the right structure and you need to use equals() not == to compare Strings. The basic structure you want is:

System.out.printf("\nInput employee name or stop to exit: ");
String employeeName = input.nextLine();
while (!employeeName.equals("stop")) {
  ...
  System.out.printf("\nInput employee name or stop to exit: ");
  employeeName = input.nextLine();
}

Basically you need to check if the user entered "stop" immediately and the above does that. Your version doesn't.

On a side note, try and adopt Java coding conventions including spacing and curly brace placement.

cletus
Why the downvote?
cletus
Thanks for the input about the while loop and the programming conventions! I didn't realize my spacing and placement is different from it.
bow
+7  A: 

I'm guessing it's because the != test you use in the while loop compares strings for reference equality. That is, when it makes a comparison, it's not just testing to see whether the strings have the same sequence of characters; it checks to see if they are the exact same object. But when the Scanner creates a String to contain the text it read from standard input, that String is not going to be the same object as the string literal "stop" in your code. They're two objects that just happen to have the same content, but they exist at different locations in memory, so != treats them as being unequal.

Solution: start your loop like this instead:

while (!"stop".equals(employeeName)) {
David Zaslavsky
A: 

Ditto previous posters on == versus equals.

It works when you initialize employeeName to "stop" because the Java compiler sees two identical strings and re-used the object. That is:

String employeeName="stop"; if (employeeName=="stop") ... etc ...

The test evaluates to true because Java creates an object to hold the first string, then it notices that the second string is identical so it re-uses the object, and thus the == comparison is comparing an object to itself.

But if instead you read the employeeName from the keyboard or a file, now the compiler of course doesn't know what someone will type in, a new object is created to hold this value, and an == compare between two different string objects returns false.

Jay