-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.java
58 lines (52 loc) · 1.62 KB
/
Console.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
/** COSC-1337-001--Jennifer Ayala--07/29/2019--Final Project:
* Create an application that reads and converts English sentences to a secret
* code according the specs given; this class process user interaction methods
* Console.java
*/
package wordconverter;
import java.util.Scanner;
/**
* The <code>Console</code> class provides methods to obtain user input
* @author Jennifer Ayala
* @version 1.0.0
*/
public class Console {
private static Scanner sc = new Scanner(System.in);
public static void displayLine() {
System.out.println();
}
public static void displayLine(String s) {
System.out.println(s);
}
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.nextLine();
return s;
}
public static int getInt(String prompt) {
int i = 0;
while (true) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer. Try again.");
}
}
return i;
}
public static double getDouble(String prompt) {
double d = 0;
while (true) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal. Try again.");
}
}
return d;
}
}