I wrote this code so that if the user press enter then it will throws the custom exception other wise it will return the user entry. But this code is throwing the custom exception anyway weather it is blank or string when I first run this code.Dont know how to fix this.
public static String validEmpty(Scanner sc, String prompt)
{
String userEntry = null;
Boolean isValid = true;
while (isValid)
{
System.out.print(prompt);
try
{
if ((userEntry=sc.nextLine()).isEmpty())
throw new CustomException();
else
return userEntry;
}
catch (InputMismatchException e)
{
sc.nextLine();
System.out.println("Error! Name must be alphabets only!");
}
catch (CustomException e)
{
sc.nextLine();
System.out.println("Error! Entry Cannot Be blank!");
}
}
return userEntry;
}