0

When user input value pay rate, it will check the value pay rate with the array of pay rate. If correct then it will prompt if user has another process need to be done. If not, it will go to calculation.

If the pay rate is wrong, then it will display wrong and ask the user to enter value again.

I have problems where after user press the 'Y' it will display 'the wrong pay rate, please try again'.

public static void main(String[] args) {

    //Scanner
    Scanner read = new Scanner(System.in);

    //Array of payRate Default
    double[] payRateDefault = {3.50, 4.00, 4.50, 4.75, 5.00, 5.25, 5.50, 5.75, 6.00};

    double payRateEntered;
    boolean isPayRate = false;

    char anotherProcess;

    System.out.print("Enter hours work: ");
    int hoursWork = read.nextInt();

    do {
        System.out.print("Enter pay rate: ");
        payRateEntered = read.nextDouble();

        for (int i = 0; i < payRateDefault.length; i++) {

            if (payRateDefault[i] == payRateEntered) {

                //If the payRate is true with array payRateDefault, proceed to ask if you have another employee
                System.out.println("Do you have any employee to process (Y/N)");
                anotherProcess = read.next().charAt(0);
                isPayRate = true;

                //Check if Y or N
                switch (anotherProcess) {
                    case 'Y':
                        //Proceed back to prompt user to enter pay rate

                        break;
                    case 'N':
                        //Proceed to calculation

                        break;
                    default:
                        //If wrong input
                        System.out.println("Please use Y or N only");
                        break;
                }
            } else {
                isPayRate = false;
            }
        }
        System.out.println("You have entered the wrong pay rate. Please try again");
    } while (!isPayRate);
}

Result:

enter image description here

Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • 3
    Just for the record: payRateDefault[i] == payRateEntered ... not a good idea for floating point numbers. See https://stackoverflow.com/questions/17898266/why-cant-we-use-to-compare-two-float-or-double-numbers – GhostCat May 16 '19 at 10:40
  • @GhostCat ok sir. Thank you for the info. I will try to change it – Ticherhaz FreePalestine May 16 '19 at 10:42
  • 2
    Why not debug it to see how the program is executed? This is how you should typically find and correct bugs. – M A May 16 '19 at 10:49

2 Answers2

2

You should execute the System.out.println("You have entered the wrong pay rate. Please try again"); line, only if isPayRate is false.

boolean finished = false;
do {
    System.out.print("Enter pay rate: ");
    payRateEntered = read.nextDouble();

    for (int i = 0; i < payRateDefault.length; i++) {

        if (payRateDefault[i] == payRateEntered) {

            //If the payRate is true with array payRateDefault, proceed to ask if you have another employee
            System.out.println("Do you have any employee to process (Y/N)");
            anotherProcess = read.next().charAt(0);
            isPayRate = true;

            //Check if Y or N
            switch (anotherProcess) {
                case 'Y':
                    //Proceed back to prompt user to enter pay rate

                    break;
                case 'N':
                    //Proceed to calculation
                    finished = true;
                    break;
                default:
                    //If wrong input
                    System.out.println("Please use Y or N only");
                    break;
            }
            break;
        } else {
            isPayRate = false;
        }
    }
    if (!isPayRate) {
        System.out.println("You have entered the wrong pay rate. Please try again");
    }
} while (!finished);
Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
  • I tried already sir, after I press 'Y'. It still display the 'you have entered the wrong pay rate' – Ticherhaz FreePalestine May 16 '19 at 11:01
  • You need to add a break to the end of the if clause as well (just after the switch). Updated the answer. Otherwise even after you find a valid pay rate from the array, the for loop continues to compare with the rest of the elements. – Udith Gunaratna May 16 '19 at 11:04
  • It is working but 1 problem left which is how to repeat the question to enter the value pay rate after press 'Y' – Ticherhaz FreePalestine May 16 '19 at 11:08
  • If you want to repeat the question in both cases (1. when the pay rate is wrong and 2. when user entered Y), you need to use a separate variable to control the while loop. Updated the answer with a suggestion. – Udith Gunaratna May 16 '19 at 11:16
2

The following code works fine for me:

package mm.com.java.so.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaRepeat {

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        double[] defaultPayRates = { 3.50, 4.00, 4.50, 4.75, 5.00, 5.25, 5.50, 5.75, 6.00 };

        double payRate;
        boolean isValid;
        boolean isContinue;
        String next;
        int workingHour;

        do {

            isValid = false;
            isContinue = false;

            System.out.print("\nEnter hours work : ");

            workingHour = Integer.parseInt(reader.readLine());

            do {

                System.out.print("Enter pay rate: ");

                payRate = Double.parseDouble(reader.readLine());

                for (int i = 0; i < defaultPayRates.length; i++) {

                    if (defaultPayRates[i] == payRate) {
                        isValid = true;
                        // TODO : make calculation here.
                        break;
                    }
                }

                if (!isValid) {
                    System.out.println("You have entered the wrong pay rate. Please try again !!!");
                }

            } while (!isValid);

            do {

                isValid = true;

                System.out.println("\nDo you have any employee to process (Y/N)");

                next = reader.readLine();

                switch (next.toLowerCase()) {
                    case "y":
                        isContinue = true;
                        break;

                    case "n":
                        isContinue = false;
                        break;

                    default:
                        isValid = false;
                        System.out.println("Please use Y or N only.");
                        break;
                }
            } while (!isValid);

        } while (isContinue);

        // TODO : print out calculation here.

        System.out.println("\nCalculation is doing. Please wait...");
    }
}

My test results is as follows:

Enter hours work : 3
Enter pay rate: 2
You have entered the wrong pay rate. Please try again !!!
Enter pay rate: 5

Do you have any employee to process (Y/N)
y

Enter hours work : 2
Enter pay rate: 5

Do you have any employee to process (Y/N)
z
Please use Y or N only.

Do you have any employee to process (Y/N)
N

Calculation is doing. Please wait...