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,
!"N".equals(sYesNo) || !"Y".equals(sYesNo)is always true, because your input is always either notNor notYif (!"N".equals(sYesNo) && !"Y".equals(sYesNo))