-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidation.java
More file actions
51 lines (46 loc) · 1.34 KB
/
Validation.java
File metadata and controls
51 lines (46 loc) · 1.34 KB
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
/**
* Class to validate user inputs across the whole project
* @author Sam
*/
public abstract class Validation {
/**
* Check if an input is a valid integer
* @param toValidate to be validated
* @return true if integer conversion succeeds, false otherwise
*/
public static boolean isInteger(String toValidate) {
try { //Try to convert input to integer
Integer.parseInt(toValidate);
return true;
} catch (NumberFormatException e){
System.out.println("Please enter an integer"); //Notify user of invalid input
return false;
}
}
public static boolean isDouble(String toValidate) {
try{
Double.parseDouble(toValidate);
return true;
} catch (NumberFormatException e) {
System.out.println("Please enter a number");
return false;
}
}
/**
* Check if an input is in a valid range
* @param lower bound of range (inclusive)
* @param upper bound of range (inclusive)
* @param toValidate to be validated
* @return true if in range, false otherwise
*/
public static boolean isRangeValid(int lower, int upper, int toValidate) {
//validate range for integers
if (toValidate <= upper && toValidate >= lower) {
return true;
}
else {
System.out.println("Please enter an integer in the specified range"); //Notify user of invalid input
return false;
}
}
}