Answer the question
In order to leave comments, you need to log in
What are the options to write the code correctly?
In addition to the while (true) loop and break statements, can't you do it? What other options are there?
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
User user = new User(); // Создаем обьект юзера
System.out.println("Write you Name..");
String name = scanner.nextLine();
user.setName(name);
System.out.println(name + " write you age..");
String age = scanner.nextLine();
try {
int userage = Integer.parseInt(age);
user.setAge(userage);
}catch (NumberFormatException e){
System.out.println("Error age"); // Перенаправить на String age = scanner.nextLine();
}
System.out.println(user);
}
}
Answer the question
In order to leave comments, you need to log in
Seems like it should be okay.
while (true) {
String age = scanner.nextLine();
try {
int userage = Integer.parseInt(age);
user.setAge(userage); break;
} catch (NumberFormatException e) {
System.out.println("Error age try again");
}
}
System.out.println(user);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question