1

I have two classes in this small program. The first part of the program works fine.

public class PayCheck
{
    private double hourlyRate, hoursWorked, grossPay, netPay; 
    final double federalTaxAmount = 0.15;
    final double stateTaxAmount = 0.09;
    final double FICAAmount = 0.07;
    private String lastName, firstName;

    public PayCheck()
    {
        hourlyRate = hoursWorked = grossPay = netPay = 0;
        lastName = firstName = "unknown";
    }

    public PayCheck(double inRate, double inHours, String inLast, String inFirst)
    {
        hourlyRate = inRate;
        hoursWorked = inHours;
        lastName = inLast;
        firstName = inFirst;
    }

    public String setLastName()
    {
        return lastName;
    }

    public String setFirstName()
    {
        return firstName;
    }

    public double setHourlyRate()
    {
        if (hourlyRate > 5)
        {
            if (hourlyRate < 100)
            {
                hourlyRate = hourlyRate;
            }
        }
        else
        {
             hourlyRate = 0;
        }

        return hourlyRate;
    }

    public double setHoursWorked()
    {
        if (hoursWorked < 0)
            if (hoursWorked > 100)
            {
                hoursWorked = hoursWorked;
            }
        else
        {
            hoursWorked = 0;
        }

        return hoursWorked;
   }

    public double getGross()
    {
        grossPay = hoursWorked * hourlyRate;
        return grossPay;
    }

    public double getNet()
    {
        netPay = grossPay - (grossPay * federalTaxAmount) - (grossPay * stateTaxAmount) - (grossPay * FICAAmount);
        return netPay;
    }

    public String toString()
    {
        return "Last Name: " + lastName + "\nFirst Name: " + firstName + "\nGross Pay: "
        + this.getGross() + "\nNet Pay: " + this.getNet() + "\nFederal Tax Amount: " 
        + (grossPay * federalTaxAmount) + "\nState Tax Amount: " + (grossPay * stateTaxAmount) 
        + "\nFICA Amount: " + (grossPay * FICAAmount);
    }
}

And then the other part that references the code above has to be in a loop and that loop never ends even though the input is the correct input to terminate the loop.

import java.util.Scanner;
public class Driver
{
    public static void main(String[] args)
    {
        Scanner key = new Scanner(System.in);
        String lastName, firstName, choice;
        double hours, rate;
        boolean again = true;
        PayCheck emp1;

        while(true)
        {
            System.out.print("Please enter the Last Name: ");
            lastName = key.next();

            System.out.print("Please enter the First Name: ");
            firstName = key.next();

            System.out.print("Please enter the number of hours: ");
            hours = key.nextDouble();

            System.out.print("Please enter the pay rate per hour: ");
            rate = key.nextDouble();

            emp1 = new PayCheck(rate, hours, lastName, firstName);

            System.out.println(emp1.toString());

            System.out.print("Do you want to another entry (Y/N)? ");
            choice = key.next();

            if (choice.toLowerCase() == "n")
            {
                break;
            }            
        }
    }
}

Is there a better way to terminate the loop, because this loop is no terminating at all.

Jdmon1998
  • 31
  • 6
  • Your loop never stops because you never never never use == to compare strings. Use .equals("n") instead. – GhostCat Feb 08 '17 at 13:29
  • There are two questions buried in this. If the OP reflects that in the title, we could vote to reopen. – Bathsheba Feb 08 '17 at 13:33

2 Answers2

2

Do not compare Strings in Java with ==. You have to use String.equals for that, for example choice.toLowerCase().equals("n").

lukeg
  • 4,189
  • 3
  • 19
  • 40
1

choice.toLowerCase() == "n" compares string references, not the contents.

Use "n".equals(choice.toLowerCase()), or similar, to compare contents.

You could replace your ostensibly infinite loop with

do {

} while (!"n".equals(choice.toLowerCase()))

This, of course, leaks the scope of choice outside the loop, but you're doing that anyway.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483