-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputReader.java
More file actions
75 lines (65 loc) · 1.75 KB
/
InputReader.java
File metadata and controls
75 lines (65 loc) · 1.75 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
import java.util.Scanner;
/**
* InputReader reads typed text input from the standard text terminal.
* The text typed by a user is returned.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1 (2016.02.29)
*
* Modified by Derek Peacock 13/12/2020
*/
public class InputReader
{
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Read a line of text from standard input (the text terminal),
* and return it as a String.
*
* @return A String typed by the user.
*/
public String getString(String prompt)
{
String inputLine = null;
boolean isValid = false;
while(!isValid)
{
System.out.print(prompt); // print prompt
inputLine = reader.nextLine();
if(!inputLine.isEmpty())
isValid = true;
else
System.out.println("\nYour input is blank!\n");
}
return inputLine;
}
/**
* Read a the next int from standard imput (the text terminal),
* and return it as an interger.
*
* @return Integer typed by user.
*/
public int getInt(String prompt)
{
int number = 0;
boolean isValid = false;
while(!isValid)
{
System.out.print(prompt); // print prompt
number = reader.nextInt();
if(number > 0)
{
isValid = true;
}
else
System.out.println("Your value is less than zero!");
}
return number;
}
}