
Patrick B. answered 09/13/19
Math and computer tutor/teacher
You can do interactive IO from the console using the console object.... here is a class that
allows standing keyboard input for a string, integer, or floating point value...
import java.io.*;
public class InputOutputIO
{
private Console console;
private boolean input_error_flag;
InputOutputIO()
{
console = System.console();
input_error_flag = false;
}
public String promptForString( String promptMsg)
{
String inbuff;
input_error_flag = false;
System.out.println("**************************************");
System.out.print(promptMsg);
try
{
inbuff = console.readLine();
}
catch (Exception ex)
{
inbuff = null;
input_error_flag = true;
System.out.println(" Input exception error has occured ");
}
return(inbuff);
}
public long promptForInteger( String promptMsg)
{
input_error_flag = false;
String inbuff = this.promptForString(promptMsg);
if (inbuff != null)
{
return(Long.parseLong(inbuff));
}
else
{
return(0);
}
}
public double promptForAmount( String promptMsg)
{
input_error_flag = false;
String inbuff = this.promptForString(promptMsg);
if (inbuff != null)
{
return(Double.parseDouble(inbuff));
}
else
{
return(0);
}
}
public static void main( String args[])
{
InputOutputIO myIO = new InputOutputIO();
String nameStr = myIO.promptForString(" What is your name ??? :>");
long longIntNum = myIO.promptForInteger(" Please input an integer :> ");
double dblFlAmt = myIO.promptForAmount(" Please input an amount :> ");
System.out.println(" Hello " + nameStr);
System.out.println(" long int # = " + longIntNum);
System.out.println(" amount = "+ dblFlAmt );
}
}