This repository was archived by the owner on Aug 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScan.java
More file actions
62 lines (52 loc) · 1.22 KB
/
Scan.java
File metadata and controls
62 lines (52 loc) · 1.22 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
import java.util.Scanner;
public class Scan{
private static Scan scan = null;
private static Scanner scanner = null;
protected Scan(){
try{
this.scanner = new Scanner(System.in);
}catch(Exception e){
System.out.println("Unable to instantiate Scanner! Check console output");
e.printStackTrace();
}
}
public static Scan getInstance(){
if(scan == null){
scan = new Scan();
}
return scan;
}
public void close(){
try{
this.scanner.close();
}catch(Exception e){
System.out.println("Unable to close Scanner! Check console output");
}
}
public static Scanner getScanner(){
if(scanner == null){
throw new RuntimeException("attempting to retrieve null scanner! Make sure to call Scan.getInstance() prior to retrieving scanner!");
}
return scanner;
}
public int getInt(){
int number = -1;
try{
number = scanner.nextInt();
}catch(Exception e){
// some type of i/o or class error
System.out.println("Error reading number input!");
}
scanner.nextLine();
return number;
}
public String getString(){
String string = null;
try{
string = scanner.nextLine();
}catch(Exception e){
System.out.println("Error reading text input!");
}
return string;
}
}