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
package Torello.Java;

import java.util.*;

/**
 * The class <CODE>'Q'</CODE> (Query) is an extremely simple debugging tool that is similar
 * to an IDE's 'breakpoint' feature, but instead using terminal input.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=Q>
 */
@Torello.JavaDoc.StaticFunctional
public class Q
{
    private Q() { }

    /**
     * <B STYLE='color:red;'><CODE>BP: Break Point</CODE></B>
     * 
     * <BR /><BR />Asks the user at the Command Line if Program Flow should continue.  If the user
     * responds with 'no', then program execution is halted.
     */
    public static void BP()
    { if (! YN("Continue ?")) System.exit(1); }

    /**
     * <B STYLE='color:red;'><CODE>BP: Break Point</CODE></B>
     * 
     * <BR /><BR />Asks the user at the Command Line if Program Flow should continue.  If the user
     * responds with 'no', then program execution is halted.
     * 
     * @param message This is printed to terminal using {@code System.out.println(message)} before
     * asking the user if he would like to continue.
     */
    public static void BP(String message)
    {
        System.out.println(message);

        if (! YN("Continue ?")) System.exit(1);
    }

    /**
     * {@code java.util.Scanner} is not the most memorable class, but still, pretty useful.
     * This {@code static} method will repeatedly prompt the user using {@code System.out} until a
     * {@code 'y'} or an {@code 'n'} has been pressed.
     *
     * @param yesOrNoQuestion This is output to the prompt each time the user fails to enter
     * {@code 'y'} or {@code 'n'}.
     *
     * @return {@code TRUE} if the user pushed {@code 'y'} on a single input line, and {@code FALSE}
     * if the user has pushed {@code 'n'} on a single input line.  The question asked will be
     * repeated if neither of those has occurred.
     */
    public static boolean YN(String yesOrNoQuestion)
    {
        Scanner input = new Scanner(System.in);

        while (true)
        {
            System.out.println(yesOrNoQuestion);
            System.out.println("Please type the letter 'y' or the letter 'n' and [ENTER]");

            String yesOrNo = input.nextLine().toLowerCase();

            if (yesOrNo.equalsIgnoreCase("y")) return true;
            if (yesOrNo.equalsIgnoreCase("n")) return false;
        }
    }

    /**
     * This will query a user for a valid input integer.  If the user fails to enter a valid
     * integer, and the {@code Integer.parseInt(user-input);} throws an exception, the method
     * will ask the user repeatedly until a valid integer has been provided.
     *
     * @return The integer version of the user's input.
     */
    public static int intNUM()
    {
        Scanner input = new Scanner(System.in);

        while (true)
        {
            System.out.println("Please enter any valid integer, and then press [ENTER]");

            String s = input.nextLine();

            try
                { int n = Integer.parseInt(s); return n; }

            catch (Exception e)
                { System.out.println("That was not a valid integer, try again."); }
        }
    }

    /**
     * Reads a single line of user input, and returns that line as a string.
     * @return <SPAN STYLE="color: green;">return (new Scanner(System.in)).nextLine();</SPAN>
     */
    public static String readLine()
    { return (new Scanner(System.in)).nextLine(); }
}