-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
114 lines (101 loc) · 3 KB
/
Utils.java
File metadata and controls
114 lines (101 loc) · 3 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
package APCSA.APCSA_Code_Your_Own;
import java.lang.Math;
import java.util.Scanner;
public class Utils {
private static Scanner scanner = new Scanner(System.in);
public static int randInt(int min, int max) {
return (int) (Math.random() * (max - min + 1)) + min;
}
public static String inputString(String question) {
System.out.print(question);
return scanner.nextLine();
}
public static String inputString(String question, String[] options) {
boolean inputContained = false;
String input = "";
while (!inputContained) {
System.out.print(question);
input = scanner.nextLine();
for (String option : options) {
if (input.equals(option)) {
inputContained = true;
}
}
}
return input;
}
public static String inputString(String question, String[] options, String invalidOption, boolean caseSensitive) {
boolean inputContained = false;
System.out.print(question);
String input = "";
input = scanner.nextLine();
if (!caseSensitive) {
input = input.toLowerCase();
}
for (String option : options) {
if (input.equals(option.toLowerCase())) {
inputContained = true;
}
}
while (!inputContained) {
System.out.println(invalidOption);
System.out.print(question);
input = scanner.nextLine();
for (String option : options) {
if (input.equals(option)) {
inputContained = true;
}
}
}
return input;
}
public static int inputInt(String question) {
System.out.print(question);
while (true) {
try {
String input = scanner.nextLine();
return Integer.parseInt(input);
} catch (Exception e) {
System.out.print("Please enter an integer: ");
}
}
}
public static int inputInt(String question, int lowLimit, int highLimit) {
System.out.print(question);
while (true) {
try {
String input = scanner.nextLine();
if (Integer.parseInt(input) < lowLimit || Integer.parseInt(input) > highLimit) {
throw new Exception("Please enter a valid number.");
}
return Integer.parseInt(input);
} catch (Exception e) {
System.out.print("Please enter an integer between " + lowLimit + " and " + highLimit + ": ");
}
}
}
public static double inputDouble(String question) {
System.out.print(question);
while (true) {
try {
String input = scanner.nextLine();
return Double.parseDouble(input);
} catch (Exception e) {
System.out.print("Please enter a number: ");
}
}
}
public static boolean twoDimensionalArrayInBounds(int row1, int column1, int row2, int column2, int outerLength,
int innerLength) {
if (row1 < 0 || column1 < 0 || row2 < 0 || column2 < 0) {
return false;
}
if (row1 >= outerLength || row2 >= outerLength) {
return false;
}
if (column1 >= innerLength || column2 >= innerLength) {
return false;
}
return true;
}
}