This thread will be an attempt to develope a SIMPLE (and good enough) class
for beginning Java students, to ease their coding
for valid and crash proof, user (Console type) input,
(and also handle Console output.)
We assume here that beginning students need not worry much,
about obtaining the fastest possible IO,
with respect to the console,
in their typical beginning student type problems,
(Note that IO is often the big bootle-neck
to the total time it takes to execute a typical student type program,
since printing to screen is slow,
and there are even longer waits for a user to enter prompted data,
and thus the very significant added time to wait here,
for user to finally press the (data) Enter key.)
Thus, we are NOT overly concerned here about speed.
Simplicity and ease of understanding the code is the aim here ...
And thus, beginning coders can fairly easily adapt,
(i.e. easily add new methods),
so that this IO class will be very useful
to ease their IO coding in many,
if not most all of,
their student coding problems.
Credits:
With thanks to 'PulsarScript' for:
https://www.daniweb.com/programming/software-development/threads/506093/java-output
and to 'sarah_15' for:
https://www.daniweb.com/programming/software-development/threads/506097/java-program
and to Dani's own 'JC' for his inspiration for this thread.
Ok ... here is a simple Console class
that students might like to use
to ease and simplify handling (most) all (of) their Console IO:
// this version: 2016-09-29 //
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner( System.in );
public static String takeInLine( String prompt ) {
System.out.print( prompt );
return sc.nextLine();
}
public static int takeInInt( String prompt ) {
while( true ) {
try {
return Integer.parseInt( takeInLine( prompt ) );
} catch( NumberFormatException e ) {
System.out.println( "Error! Invalid integer. Try again." );
}
}
}
public static long takeInLong( String prompt ) {
while( true ) {
try {
return Long.parseLong( takeInLine( prompt ) );
} catch( NumberFormatException e ) {
System.out.println( "Error! Invalid long. Try again." );
}
}
}
public static double takeInDouble( String prompt ) {
while( true ) {
try {
return Double.parseDouble( takeInLine( prompt ) );
} catch( NumberFormatException e ) {
System.out.println( "Error! Invalid decimal. Try again." );
}
}
}
// Call as: more( "Y" ); // to have default case of 'Yes more' //
// Call as: more( "N" ); // to have default case of 'No more' //
public static boolean more( String defaultStr ) {
String reply = takeInLine( "More (y/n) ? " );
if( defaultStr.equalsIgnoreCase( "Y" ) ) {
if( reply.equalsIgnoreCase( "N" ) ) {
return false;
} else {
return true;
}
} else {
if( reply.equalsIgnoreCase( "Y" ) ) {
return true;
} else {
return false;
}
}
}
// defaults to 'Yes more' //
public static boolean more() {
return more( "Y" );
}
public static void println() {
System.out.println();
}
public static void println( String s ) {
System.out.println( s );
}
public static void print( String s ) {
System.out.print( s );
}
public static void format( String formatStr, Object ... obj ) {
System.out.format( formatStr, obj );
}
}
And now a little test program to try it out:
// this version: 2016-09-29 //
class ConsoleTest {
public static void main( String[] args ) {
do {
String name = Console.takeInLine( "Enter this user name : " );
int pin = Console.takeInInt( "Enter pin (8 digits) : " );
long id = Console.takeInLong( "Enter ID (16 digits) : " );
double amount = Console.takeInDouble( "Enter $ amount owing : " );
Console.println();
Console.format( "%s, %d, %d owes: %,1.2f dollars. %n", name, pin, id, amount );
Console.println();
Console.println( "On to next client?" );
Console.print( "Whenever you are ready, enter y or n, " );
Console.println( "or just press the Enter key for 'y'." );
} while( Console.more() );
}
}