-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.java
More file actions
155 lines (130 loc) · 4.89 KB
/
Copy pathInput.java
File metadata and controls
155 lines (130 loc) · 4.89 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package util;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import java.util.Scanner;
public class Input {
private Scanner myScanner;
// new constructor for scanner
public Input () {
// scanner property from above is set to a new instance of the Scanner class
myScanner = new Scanner(System.in);
}
public String getString() {
// System.out.println("Please enter a string:");
// adding extra line of text to binary and hex functions
return myScanner.nextLine();
}
public String getString(String prompt) {
System.out.println(prompt);
return getString();
}
// public boolean yesNo() {
// System.out.println("Please enter yes or no:");
// String userInput = myScanner.next();
// return userInput.equalsIgnoreCase("yes") || userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("sure");
// }
// or
public boolean yesNo() {
String userInput = myScanner.nextLine();
return userInput.equalsIgnoreCase("yes") || userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("sure");
}
public boolean yesNo(String prompt) {
System.out.println(prompt);
String userInput = myScanner.next();
return userInput.equalsIgnoreCase("yes") || userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("sure");
}
// public int getInt(int min, int max) {
// System.out.println("please enter a value to test if valid between %d and %d.\n:");
// int userInput = getInt();
// if (userInput >= min && userInput <= max) {
// System.out.printf("%d is between %d and %d\n", userInput, min, max);
// } else {
// System.out.println("Not a valid number.");
// getInt(min, max);
// }
// return userInput;
// }
public int getInt(String prompt) {
System.out.println(prompt);
return getInt();
}
// public int getInt(int min, int max, String prompt) {
// return getInt(min, max, "Please enter an int between" + min + " and " + max);
// int input;
// do{
// System.out.printf("Please enter an int between %d and %d.", min, max);
// input = this.myScanner.nextInt();
// }while(input < min || input > max);
//
// return input;
// }
public int getInt () {
try {
System.out.println("Working!");
return Integer.parseInt(getString());
} catch (NumberFormatException e) {
System.out.println(e.toString());
// e.printStackTrace(System.out);
return getInt("Please enter an integer");
}
}
// public double getDouble(double min, double max) {
// double userInput = getDouble();
// if(userInput > min && userInput < max) {
// System.out.println("number is valid");
// return userInput;
// } else {
// System.out.println("number is not valid");
// return getDouble(min, max);
// }
// }
public double getDouble(String prompt) {
System.out.println(prompt);
return getDouble();
}
public double getDouble(double min, double max) {
double input;
do{
System.out.printf("Please enter an int between %f and %f.", min, max);
input = this.myScanner.nextInt();
}while(input < min || input > max);
return input;
}
public double getDouble() {
try {
// System.out.println("Working!");
return Double.parseDouble(getString());
// putting valueOf here will cause intellij to ask if you want to use parseDouble instead
} catch (RuntimeException e) {
System.out.println(e.toString());
e.printStackTrace();
return getDouble("Please enter a double (number with decimals).");
}
}
public int getBinary() {
System.out.println("Please enter a number.");
try {
// System.out.println("Working!");
return Integer.parseInt(getString(), 2);
// valueOf doesn't work in this specific setup, need to figure that out
} catch (NumberFormatException e) {
System.out.println(e.toString());
System.out.println("Please re-enter a number.");
e.printStackTrace();
return getBinary();
}
}
public int getHex() {
System.out.println("Please enter a number.");
try {
// System.out.println("Working!");
return Integer.parseInt(getString(), 16);
// valueOf doesn't work in this specific setup, need to figure that out
} catch (NumberFormatException e) {
System.out.println(e.toString());
System.out.println("Please re-enter a number.");
e.printStackTrace();
return getHex();
}
}
}