Package Torello.Java
Class Q
- java.lang.Object
-
- Torello.Java.Q
-
public class Q extends java.lang.Object
The class'Q'
(Query) is an extremely simple debugging tool that is similar to an IDE's 'breakpoint' feature, but instead using terminal input.
The letter Q refers to Questions - as in Shell input questions. The intention is to add a few "pre-written" yes/no or accept/reject methods, just like some of the provided Window-Event Questions in JavaScript.
The follwing example will print a brief message about the value of a variable in his or her program, and then print a message asking the user if he would like to continue the execution-path, or exit the JVM.
Java Line of Code:
Q.BP("Current Value of X: [" + x + ']');
Hi-Lited Source-Code:- View Here: Torello/Java/Q.java
- Open New Browser-Tab: Torello/Java/Q.java
File Size: 3,430 Bytes Line Count: 100 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctional
Annotation may also be called 'The Spaghetti Report'.Static-Functional
classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@Stateless
Annotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 5 Method(s), 5 declared static
- 0 Field(s)
-
-
Method Summary
Program Execution Break Point, Query the User Modifier and Type Method static void
BP()
static void
BP(String message)
Query a User (at the terminal) a Yes / No Answer Modifier and Type Method static boolean
YN(String yesOrNoQuestion)
Query a User (at the terminal) an Integer Modifier and Type Method static int
intNUM()
Quick Scanner String Read-Line Modifier and Type Method static String
readLine()
-
-
-
Method Detail
-
BP
public static void BP()
BP: Break Point
Asks the user at the Command Line if Program Flow should continue. If the user responds with 'no', then program execution is halted.
-
BP
public static void BP(java.lang.String message)
BP: Break Point
Asks the user at the Command Line if Program Flow should continue. If the user responds with 'no', then program execution is halted.- Parameters:
message
- This is printed to terminal usingSystem.out.println(message)
before asking the user if he would like to continue.- Code:
- Exact Method Body:
System.out.println(message); if (! YN("Continue ?")) System.exit(1);
-
YN
public static boolean YN(java.lang.String yesOrNoQuestion)
java.util.Scanner
is not the most memorable class, but still, pretty useful. Thisstatic
method will repeatedly prompt the user usingSystem.out
until a'y'
or an'n'
has been pressed.- Parameters:
yesOrNoQuestion
- This is output to the prompt each time the user fails to enter'y'
or'n'
.- Returns:
TRUE
if the user pushed'y'
on a single input line, andFALSE
if the user has pushed'n'
on a single input line. The question asked will be repeated if neither of those has occurred.- Code:
- Exact Method Body:
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; }
-
intNUM
public static int intNUM()
This will query a user for a valid input integer. If the user fails to enter a valid integer, and theInteger.parseInt(user-input);
throws an exception, the method will ask the user repeatedly until a valid integer has been provided.- Returns:
- The integer version of the user's input.
- Code:
- Exact Method Body:
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."); } }
-
readLine
public static java.lang.String readLine()
Reads a single line of user input, and returns that line as a string.- Returns:
- return (new Scanner(System.in)).nextLine();
- Code:
- Exact Method Body:
return (new Scanner(System.in)).nextLine();
-
-