0
static void getFirstName() {
    System.out.println("Please enter Client's first name"); 
    String sFirstName = sc.nextLine().toUpperCase();
    monthlyRates.setFirstName(sFirstName);   
        System.out.println("You wrote " 
                + sFirstName 
                + " is that correct? \n"
                + "Y or N");

    String sYesNo = sc.next().toUpperCase();
        if (!"N".equals(sYesNo) && !"Y".equals(sYesNo)) {
            System.out.println("Please enter Y or N");
            //re-run this snippet. Can this be nested? 
        }else ("N".equals(sYesNo)){
            getFirstName();
        }
    }
}  

I want to add some error checking to the above. If the sFirstName is correct, confirmed by a Y, then it moves to the next section of code. If it's a N, it re-runs the code. If it's anything other than a Y or N it reruns the if.

I've looked at exceptions but am not sure if they are suitable for the above? Any help would be appreciated!

Thank you,

5
  • 2
    Don't use exceptions. Use a do-while. Commented Dec 10, 2017 at 10:52
  • I believe !"N".equals(sYesNo) || !"Y".equals(sYesNo) is always true, because your input is always either not N or not Y Commented Dec 10, 2017 at 10:53
  • 1
    It's not generally considered good code to validate input like this using Exceptions. See stackoverflow.com/a/7304405/802482 Commented Dec 10, 2017 at 10:55
  • @QBrute Correct, it should be if (!"N".equals(sYesNo) && !"Y".equals(sYesNo)) Commented Dec 10, 2017 at 10:56
  • I've changed the || to &&, that makes perfect sense! Thank you. I'm getting two errors, one on the else line saying expected ';' and the other is on the else getFirstName(); saying getFirstName(); will recurse forever! Commented Dec 10, 2017 at 11:00

1 Answer 1

0

i creaded you a couple of examples - mabye you like some of them. my personal opinion is to use exceptions as less as possible cause they interrupt your control flow very exceptional and maybe very unexcepted, they have to be catched (could be tricky on runtime exceptions - cause you have to know the code you are calling - which is not information hiding)and try catch blocks are very ugly compared to a simple if statement :-) but to the code:

private boolean confirmFirstName(String firstName) {
    System.out.println("You wrote "
            + firstName
            + " is that correct? \n"
            + "Y or N");
    final String answer = sc.next().toUpperCase();
    return answer.equalsIgnoreCase("y");
}

private String enterFirstName() {
    System.out.println("Please enter Client's first name");
    final String sFirstName = sc.nextLine().toUpperCase();
    return sFirstName;
}

private void getFirstNameV1() {
    final int maxTrials = 5;
    // loop for max trials times and break if input is correct
    for (int i = 0; i < maxTrials; i++) {
        final String firstName = enterFirstName();
        if (confirmFirstName(firstName)) {
            setMonthlyRates(firstName);
            break;
        }
    }
}

private void getFirstNameV2() {
    final String firstName = enterFirstName();
    final boolean valid = confirmFirstName(firstName);
    // use a recursive function but take care,
    // there is no exit instead of entering a valid value otherwise you are in an infinite loop
    if (valid) {
        setMonthlyRates(firstName);
    } else {
        getFirstNameV2();
    }
}

private void getFirstNameV3() {
    // first time input
    String firstName = enterFirstName();
    boolean valid = confirmFirstName(firstName);
    // if not valid loop until its valid, there is also no exit of this loop
    while (!valid) {
        firstName = enterFirstName();
        valid = confirmFirstName(firstName);
    }
    setMonthlyRates(firstName);
}

private void getFirstNameV4() {
    // first time input
    String firstName = enterFirstName();
    boolean valid = confirmFirstName(firstName);
    final int maxTrials = 5;
    // if not valid loop for max trials times
    for (int i = 0; i < maxTrials || valid; i++) {
        firstName = enterFirstName();
        valid = confirmFirstName(firstName);
    }
    if (valid) {
        setMonthlyRates(firstName);
    }
}

private void setMonthlyRates(String firstName) {
    monthlyRates.setFirstName(firstName);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I will try these out and see what outputs I get :) thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.