problem med Scanner og try catch statement
Starten af dette program koerer fint med user input fra person 1. Problemet starter ved user input fra person 2. Jeg har i linie 27 proevet med "String name2 = scan.next();" hvilket virker men saa kan man kun indtaste eet navn, og ikke f.eks fornavn plus efternavn.import java.util.Scanner;
public class Tst {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("person 1 enter your name: ");
String name = scan.nextLine();
System.out.println("person 1 enter your age: ");
int age = 0;
boolean gotIt = false;
while(!gotIt) {
try {
age = scan.nextInt();
gotIt = true;
} catch (Exception e) {
System.err.println("That is not a valid number!!!");
scan.next();
}
}
System.out.println("your name is " + name + "\n" +
"your age is " + age + "\n");
System.out.println("person 2 enter your name: ");
String name2 = scan.nextLine();
System.out.println("person 2 enter your age: ");
int age2 = 0;
boolean gotIt2 = false;
while(!gotIt2) {
try {
age2 = scan.nextInt();
gotIt2 = true;
} catch (Exception e) {
System.err.println("That is not a valid number!!!");
scan.next();
}
}
System.out.println("your name is " + name2 + "\n" +
"your age is " + age2 + "\n");
}
}