0

Below is my code, basically what I want the program to do is loop the while statement every time the enter key is hit, and then when "q" is submitted to the console, I want the program to end. What did I do wrong? With this code, it only runs once, and if i change

if (moreEmps != "q") break; 

to

if (moreEmps == "q") break;

then it will continue no matter what is entered. can someone steer me in the right direction? Thanks!

    import java.text.NumberFormat;
    java.util.Scanner;
    /**
    * Input a number of hours and calculate the pay at $10.00 per hour
    * Pay time and one half for any hours over 40.
    */
    public class PayCalc
    {
    public static void main (String[] args)
    {
    // Declare constants to be used in payroll calculation
    final double RATE = 10.00;     // hourly rate
    final int REGULAR_HOURS = 40; // hours paid at regular rate

    double pay = 0.0;
    Scanner input = new Scanner (System.in);

    System.out.println("Employee Payroll Program");
    System.out.println("Press enter to begin.");

    String moreEmps = "";

    while (moreEmps != input.nextLine())
    {
         System.out.print("Enter employee name: ");
         String employee = input.next();
         System.out.print("Enter hours worked:  ");
         int hours = input.nextInt();

         if (hours > REGULAR_HOURS)

            pay = RATE * REGULAR_HOURS + (hours - REGULAR_HOURS) * RATE * 1.5;

         else
            pay = hours * RATE;

         NumberFormat formit = NumberFormat.getCurrencyInstance();
         System.out.println();
         System.out.print("Total Pay for "); 
         System.out.print(employee);
         System.out.println(" : " + formit.format(pay));

         System.out.println("Any more employees?");
         System.out.println("Press enter to input another employee's information, or q to quit.");
         moreEmps = input.next();
         moreEmps.toLowerCase();  

         if (moreEmps != "q") break;
    } 


}
}
  • As you noticed problem seems to be in conditions, which means in way you are comparing Strings. Have you tried searching "[how Strings in Java should be compared](https://www.google.com/search?q=how%20Strings%20in%20Java%20should%20be%20compared)"? – Pshemo Mar 28 '14 at 16:19
  • Thank you! sorry for the repost, I didn't think of the way they were compared as the problem – user3473389 Mar 28 '14 at 16:26

1 Answers1

0

You need to compare strings, using equals().

dognose
  • 20,360
  • 9
  • 61
  • 107