📄 kbr.java
字号:
// This class can be used to enter single character and numerical
// values at the Keyboard, as described in Appendix G of the text.
import java.io.*; // needed to access input stream classes
public class KBR
{
// This method sets up the basic keyboard input streams
// and reads a line of characters from the keyboard.
// It returns all characters entered as a string, with any
// entered whitespace included.
public static String readData()
throws java.io.IOException
{
// set up the first input stream, which is
// needed for conversion capabilities
InputStreamReader isr = new InputStreamReader(System.in);
// set up a buffered stream, which is
// needed to access readLine()
BufferedReader br = new BufferedReader(isr);
return br.readLine(); // read and return the entered data
}
// This method captures and returns the first character entered
// at the keyboard. If no characters are entered, it will return
// the code for the ENTER key.
public static char readChar()
throws java.io.IOException
{
String inString = null;
char key;
inString = readData();
if (inString.length() == 0)
key = 0x000D; // Unicode value for the Enter key
else
key = inString.charAt(0); // 1st character entered
return key;
}
// This method attempts to convert the characters entered at the
// keyboard to an integer value. If the conversion cannot
// be done, an error message is displayed and the read is
// continued until a valid integer is entered.
public static int readInt()
throws java.io.IOException
{
int inValue = 0; // must initialize the variable
boolean validNumber = false;
String inString = null;
while(!validNumber)
{
try
{
inString = readData();
inValue = Integer.parseInt(inString.trim());
validNumber = true;
}
catch(NumberFormatException e)
{
System.out.println(" The value you entered is not valid. ");
System.out.println(" Please enter only numeric digits.");
System.out.print("Enter an integer value: ");
}
}
return inValue;
}
// This method attempts to convert the characters entered at the
// keyboard to a long integer value. If the conversion cannot
// be done, an error message is displayed and the read is
// continued until a valid long integer is entered.
public static long readLong()
throws java.io.IOException
{
long inValue = 0L; // must initialize the variable
boolean validNumber = false;
String inString = null;
while(!validNumber)
{
try
{
inString = readData();
inValue = Long.parseLong(inString.trim());
validNumber = true;
}
catch(NumberFormatException e)
{
System.out.println(" The value you entered is not valid. ");
System.out.println(" Please enter only numeric digits.");
System.out.print("Enter a long integer value: ");
}
}
return inValue;
}
// This method attempts to convert the characters entered at the
// keyboard to a float value. If the conversion cannot
// be done, an error message is displayed and the read is
// continued until a valid float is entered.
public static float readFloat()
throws java.io.IOException
{
float inValue = 0F; // must initialize the variable
boolean validNumber = false;
String inString = null;
while(!validNumber)
{
try
{
inString = readData();
inValue = Float.parseFloat(inString.trim());
validNumber = true;
}
catch(NumberFormatException e)
{
System.out.println(" The value you entered is not valid. ");
System.out.println(" Please enter only numeric digits");
System.out.println(" and, at most, a single decimal point.");
System.out.print("Enter a float value: ");
}
}
return inValue;
}
// This method attempts to convert the characters entered at the
// keyboard to a double value. If the conversion cannot
// be done, an error message is displayed and the read is
// continued until a valid double is entered.
public static double readDouble()
throws java.io.IOException
{
double inValue = 0; // must initialize the variable
boolean validNumber = false;
String inString = null;
while(!validNumber)
{
try
{
inString = readData();
inValue = Double.parseDouble(inString.trim());
validNumber = true;
}
catch(NumberFormatException e)
{
System.out.println(" The value you entered is not valid. ");
System.out.println(" Please enter only numeric digits");
System.out.println(" and, at most, a single decimal point.");
System.out.print("Enter a double value: ");
}
}
return inValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -