The problem is that the 'next' method returns the next whitespace surrounded token, i.e. if you enter "Joe Bob" it only returns "Joe". The 'nextInt' method thereafter tries to intepret "Bob" as an integer.
You could do something like this:
Code:
System.out.println("Enter a person's name: ");
name = sc.next();
if (!sc.hasNextInt()) {
name += " " + sc.next();
}
System.out.println(name + " enter your age: ");
age = sc.nextInt();
Easier and more robust would be just to read a complete line as the name:
Code:
System.out.println("Enter a person's name: ");
name = sc.nextLine();
System.out.println(name + " enter your age: ");
age = sc.nextInt();
Cheers,
Martin.
Bookmarks