Answer the question
In order to leave comments, you need to log in
How to handle the following exceptions?
How to handle the following exceptions?
I need to handle an exception if the user entered numbers instead of letters and typed
public class Employee {
public void readData(){
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Введите имя: ");
String name = scanner.nextLine();
System.out.println("Введите фамилию:");
String lastName = scanner.nextLine();
System.out.println("Введите дата рождения: ");
int birthDate = scanner.nextInt();
System.out.println("Введите начало год работы: ");
int startWorking = scanner.nextInt();
} catch (Exception exception){
System.out.println("Введите коректные данные!");
}
}
}
Answer the question
In order to leave comments, you need to log in
Several options for solving the problem immediately come to mind:
1) Use Pattern and a regular expression so that the entered data is correct. Relatively speaking, the user can enter only alphabetic characters for the full name, and only numbers for the date of birth.
You can see an example of the implementation of such a task at the link:
https://javatutorialhq.com/java/util/scanner-class...
Is this solution suitable for you?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question