-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonalData.java
More file actions
29 lines (26 loc) · 909 Bytes
/
Copy pathPersonalData.java
File metadata and controls
29 lines (26 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
/*
* This code ask user for their name and age.
* IF "fred" is entered instead age, the NumberFormatException is thrown.
* Age is then converted to integer and both values are outputed.
*/
public class PersonalData {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
String tempAge = scanner.nextLine();
scanner.close();
if(tempAge.equalsIgnoreCase(("fred"))){
throw new NumberFormatException();
}
System.out.println(name);
try{
int age = Integer.parseInt(tempAge);
System.out.println(age);
}catch(NumberFormatException exeption){
System.out.println("Wrong format");
}
}
}