forked from aneez9n/Basic-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgeInputVer2.java
51 lines (46 loc) · 1.21 KB
/
AgeInputVer2.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exceptionhandling;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
*
* @author admin
*/
public class AgeInputVer2
{
private static final String DEFAULT_MESSAGE = "Your age: ";
private Scanner scanner;
public AgeInputVer2()
{
scanner = new Scanner(System.in);
}
public int getAge()
{
return getAge(DEFAULT_MESSAGE);
}
public int getAge(String prompt)
{
int age=0;
boolean keepGoing = true;
while (keepGoing)
{
System.out.print(prompt);
try
{
age = scanner.nextInt();
keepGoing = false;
}
catch (InputMismatchException e)
{
scanner.next(); //remove the leftover garbage
//from the input buffer
System.out.println("Invalid Entry.Please enter digits only.");
}
}
return age;
}
}