📄 userinput.java
字号:
createInput();//Sets up Scanner object
try
{
String userInput = new String(input.nextLine());
returnValue = (java.lang.Long.valueOf(userInput)).longValue();
}//end try
catch(Exception e)
{
System.out.println("Exception while reading user's input as a " +
"long");
}//end catch
return returnValue;
} // end readLong()=========================================================
//==========================================================================
/**
* Reads input from the keyboard or file and returns it as a boolean
* @return the user input value
*/
//==========================================================================
public static boolean readBoolean()
{
boolean returnValue = false;
createInput();//Sets up Scanner object
try
{
String userInput = new String(input.nextLine());
returnValue = (java.lang.Boolean.valueOf(userInput)).booleanValue();
}//end try
catch(Exception e)
{
System.out.println("Exception while reading user's input as a" +
"boolean");
}//end catch
return returnValue;
} // end readLong()=========================================================
//==========================================================================
/**
* Formats an int and returns it as a String.
* @param number The int number to be formated.
* @param align The number of spaces to place before the value.
* 0 is minimum left aligned. 20 is maximum.<br>
* @return
*/
//==========================================================================
public static String formatInt (int number, int align)
{
DecimalFormat N = new DecimalFormat();
String spaces = " ";
N.setGroupingUsed(false);
N.setMaximumFractionDigits(0);
String num = N.format(number);
if (align < 0) // true implies left-justify
{
if (num.length() < -align)
{
// add trailing spaces
num = num + spaces.substring(0,-align-num.length());
}//end if
}//end if
else
{
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
}//end else
return num;
}//end formatInt ===========================================================
//============================================================================
/**
* Reformats a double data type and returns it as a String.
* @return The double value represented as formated String output
* @param frac the number of decimal places after the point.
* @param align The number of spaces to place before the value.
* 0 is minimum left aligned. 20 is maximum.<br>
* @param number The double value to be formated.
*/
//============================================================================
public static String formatDouble (double number, int align, int frac)
{
DecimalFormat N = new DecimalFormat();
String spaces = " ";
N.setGroupingUsed(false);
N.setMaximumFractionDigits(frac);
N.setMinimumFractionDigits(frac);
String num = N.format(number);
if (align < 0) // true implies left-justify
{
if (num.length() < -align)
{
// add trailing spaces
num = num + spaces.substring(0,-align-num.length());
}
}
else
{
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
}
return num;
}//end formatDouble===========================================================
//============================================================================
/**
* Reformats a float data type and returns it as a String.
* @return The double value represented as formated String output
* @param frac the number of decimal places after the point.
* @param align The number of spaces to place before the value.
* 0 is minimum left aligned. 20 is maximum.<br>
* @param number The float value to be formated.
*/
//============================================================================
public static String formatFloat (float number, int align, int frac)
{
DecimalFormat N = new DecimalFormat();
String spaces = " ";
N.setGroupingUsed(false);
N.setMaximumFractionDigits(frac);
N.setMinimumFractionDigits(frac);
String num = N.format(number);
if (align < 0) // true implies left-justify
{
if (num.length() < -align)
{
// add trailing spaces
num = num + spaces.substring(0,-align-num.length());
}
}
else
{
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
}
return num;
}//end formatDouble===========================================================
//===========================================================================
/**
* Opens the specified text file for input. This allows redirects input,
* allowing the read commands to read from a file rather than the keyboard.
* <br>
* When the <b>closeInputFile</b>< method is caled the keyboard input can
* then again be used with the various read methods.
* @param fileName The name and location of the file, i.e.
* c://folder/file.txt
* @throws IOException Thrown when a IO exception happens.
* @see closeInputFile()
*/
//===========================================================================
public static void openInputFile(String fileName)
{
try
{
if (fileInput != null)
fileInput = null;
if (input != null)
input = null;
fileInput = new File(fileName);
input = new Scanner(fileInput);//Sets up Scanner object
}//end try
catch (IOException e)
{
System.out.println("There was an error opening the input file.");
e.printStackTrace();
}//end if
}//===========================================================================
//============================================================================
/**
* Closes the input from the text file. Once closed the input methods then can
* return to reading in from the keyboard.
* @see openInputFile(String fileName)
*/
//============================================================================
public static void closeInputFile ()
{
input.close();
input = null;
}//===========================================================================
//============================================================================
/**
* Opens the specified file for output.
* @param name The file name
* @throws IOException Thrown when an error occours.
*/
//============================================================================
public static void openOutputFile(String name)
{
try
{
outputFile = new File(name);
fw= new FileWriter(outputFile);
pw = new PrintWriter(fw);
}//end if
catch (IOException e)
{
System.out.println("There was an error opening the output file.");
e.printStackTrace();
}//end if
}//===========================================================================
//============================================================================
/**
* Use this to close the output file and return the print methods from file
* printing to screen printing.
* @throws IOException
*/
//============================================================================
public static void closeOutputFile ()
{
try
{
fw.close();
pw.close();
outputFile = null;
pw = null;
fw = null;
}//end try
catch (IOException e)
{
System.out.println("There was an error closing the output file.");
e.printStackTrace();
}//end if
}//===========================================================================
//##########################################################################
// main class, used to test the various methods
//##########################################################################
public static void main(String[] argv)
{
/*
//##############################################################
//print data to the screen using Println
//##############################################################
println("===== READ INPUT FROM KEYBOARD AND OUTPUT TO SCREEN TEST ======");
prompt("Enter int: ");
int tempint = readInt();
println("Read int: " + tempint);
*/
prompt("Enter double: ");
double tempdouble = readDouble();
println("Read double: " + tempdouble);
prompt("Enter char: ");
char tempchar = readChar();
println("Read char: " + tempchar);
prompt("Enter String: ");
String tempString = readString();
println("Read String: " + tempString);
prompt("Enter short: ");
short tempshort = readShort();
println("Read short: " + tempshort);
prompt("Enter long: ");
long templong = readLong();
println("Read long: " + templong);
prompt("Enter float: ");
float tempfloat = readFloat();
println("Read float: " + tempfloat);
prompt("Enter boolean: ");
boolean tempBoolean = readBoolean();
println ("read boolean: " + tempBoolean);
//#############################################################
//Create a new file and redirect print and println to new file
//#############################################################
println("===== READ INPUT FROM KEYBOARD AND OUTPUT TO FILE TEST ======");
//Create file for output and redirect print and println to file
openOutputFile("test1.txt");
//output data to the file.
prompt("Enter int: ");
int tempint1 = readInt();
println(tempint1);
prompt("Enter double: ");
double tempdouble1 = readDouble();
println(tempdouble1);
prompt("Enter char: ");
char tempchar1 = readChar();
println(tempchar1);
prompt("Enter String: ");
String tempString1 = readString();
println(tempString1);
prompt("Enter short: ");
short tempshort1 = readShort();
println(tempshort1);
prompt("Enter long: ");
long templong1 = readLong();
println(templong1);
prompt("Enter float: ");
float tempfloat1 = readFloat();
println(tempfloat1);
prompt("Enter boolean: ");
boolean tempBoolean1 = readBoolean();
println(tempBoolean1);
closeOutputFile();//redirect println output back to the screen
/*
//##############################################################
//Read data from the input file and store in the computer
//##############################################################
println("===== READ INPUT FROM FILE AND OUTPUT TO SCREEN TEST ======");
openInputFile("test1.txt");
int tempInt2 = readInt();
println("Int from file: " + tempInt2);
double tempDouble2 = readDouble();
println("double from file: " + tempDouble2);
char tempChar2 = readChar();
println("char from file: " + tempChar2);
String tempString2 = readString();
println("String from file: " + tempString2);
short tempShort2 = readShort();
println("short from file: " + tempShort2);
long tempLong2 = readLong();
println("long from file: " + tempLong2);
float tempFloat2 = readFloat();
println("float from file: " + tempFloat2);
boolean tempBoolean2 = readBoolean();
println("boolean from file: " + tempBoolean2);
//close input file and redirect input back to the keyboard
closeInputFile();
*/
} // end main()
} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -