-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidation.java
109 lines (77 loc) · 4.01 KB
/
Validation.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package week3_methods;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Validation {
//Create two scanners
static Scanner stringScanner = new Scanner(System.in);
static Scanner numberScanner = new Scanner(System.in);
public static void main(String[] args) {
// Test the validation methods
double computerPrice = doubleInput("What is the price of a computer?");
System.out.println("Thank you, you entered $" + computerPrice);
System.out.println("Please enter the price of a cellphone");
double cellPrice = doubleInput();
System.out.println("Thank you, you entered $" + cellPrice);
int numberOfStudents = intInput("How many students in a class?");
System.out.println("Thank you, you entered " + numberOfStudents);
System.out.println("Please enter the quantity of cellphones");
int cellQuantity = intInput();
System.out.println("Thank you, you entered " + cellQuantity);
//Close scanners.
stringScanner.close();
numberScanner.close();
} // This is the end of the main method.
// A variant of the method below - notice it calls doubleInput with null as the argument
public static double doubleInput() {
return doubleInput(null);
}
//Takes a question, asks user the question, checks to make sure user enters a double, and
//then returns that double to the calling method.
public static double doubleInput(String question) {
while (true) {
// If user has provided a question, then print it for the user
if (question != null) {
System.out.println(question);
}
//Try to read what the user typed, expect it to be a double.
try {
// If the input can be read as a double, that double value will be returned
// This ends the loop, and this method, and control returns to the calling method.
return numberScanner.nextDouble();
} // if the input can't be read as a double, then an error will be raised.
// That error can be 'caught' by this code, and we can print an error message.
// Since we are inside a while loop, then the loop can repeat and ask the user for input again.
catch (InputMismatchException ime) {
System.out.println("Error - please enter a number");
numberScanner.next(); //Clear any other characters from the Scanner
}
}
}
// A variant of the method below - notice it calls intInput with null as the argument
public static int intInput() {
return intInput(null);
}
//Takes a question, asks user the question, checks to make sure user enters an int, and
//then returns that int to the calling method.
public static int intInput(String question) {
while (true) {
// If user has provided a question, then print it for the user
if (question != null) {
System.out.println(question);
}
//Try to read what the user typed as an int.
try {
// If the input can be read as a int, that int will be returned
// This ends the loop, and this method, and control returns to the calling method.
return numberScanner.nextInt();
} // if the input can't be read as an int, then an error will be raised.
// For example, if the user enters 'ten' or 1.4 or 123456543454343434, these are not ints, so will cause an error.
// That error can be 'caught' by this code, and we can print an error message.
// Since we are inside a while loop, then the loop can repeat and ask the user for input again.
catch (InputMismatchException ime) {
System.out.println("Error - please enter an integer number");
numberScanner.next(); //Clear any other characters from the Scanner
}
}
}
}