-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate.java
82 lines (74 loc) · 2.35 KB
/
Validate.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* 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 income.tax.calculator;
import java.util.Scanner;
/**
*
* @author khanh
*/
public class Validate {
Scanner in = new Scanner(System.in);
public int inputInt(String msg){
System.out.print(msg);
while(true){
try{
int input = Integer.valueOf(in.nextLine());
if(input < 1){
throw new Exception();
}
return input;
}catch(NumberFormatException ex){
System.out.println("Please enter a number");
}catch(Exception ex){
System.out.println("Number must greater than 0");
}
}
}
public int inputOption(String msg){
System.out.print(msg);
while(true){
try{
int input = Integer.valueOf(in.nextLine());
if(input < 0){
throw new Exception();
}
return input;
}catch(NumberFormatException ex){
System.out.println("Please enter a number");
}catch(Exception ex){
System.out.println("Number must greater than 0");
}
}
}
public String checkStringInput(String msg){
System.out.print(msg);
String input = in.nextLine().trim();
while(true){
if(input.isEmpty()){
System.err.println("Empty string");
System.out.print("Enter again: ");
}else{
return input;
}
}
}
public double inputDouble(String msg){
System.out.print(msg);
while(true){
try{
double input = Double.valueOf(in.nextLine());
if(input < 1){
throw new Exception();
}
return input;
}catch(NumberFormatException ex){
System.out.println("Please enter a number");
}catch(Exception ex){
System.out.println("Number must greater than 0");
}
}
}
}