📄 savitchin.java
字号:
}
else
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input must be");
System.out.println("one of the following:");
System.out.println("the word true,");
System.out.println("the word false,");
System.out.println("the letter T,");
System.out.println("or the letter F.");
System.out.println("You may use either upper-");
System.out.println("or lowercase letters.");
System.out.println("Please, try again.");
System.out.println("Enter input:");
}
}
return result;
}
/********************************************************
*Reads the next input character and returns that character. The
*next read takes place on the same line where this one left off.
********************************************************/
public static char readChar()
{
int charAsInt = -1; //To keep the compiler happy
try
{
charAsInt = System.in.read();
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
return (char)charAsInt;
}
/*******************************************************
*Reads the next nonwhite input character and returns that
*character. The next read takes place immediately after
*the character read.
*******************************************************/
public static char readNonwhiteChar()
{
char next;
next = readChar();
while (Character.isWhitespace(next))
next = readChar();
return next;
}
/**********************************************************
*The following methods are not used in the text, except for
*a brief reference in Chapter 2. No program code uses them.
*However, some programmers may want to use them.
*********************************************************/
/**********************************************************
*Precondition: The next input in the stream consists of an
*int value, possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters
*and returns the int value it represents. Discards the first
*whitespace character after the word. The next read takes
*place immediately after the discarded whitespace.
*In particular, if the word is at the end of a line, the
*next reading will take place starting on the next line.
*If the next word does not represent an int value,
*a NumberFormatException is thrown.
*********************************************************/
public static int readInt() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Integer.parseInt(inputString);
}
/**********************************************************
*Precondition: The next input consists of a long value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters and
*returns the long value it represents. Discards the first
*whitespace character after the string read. The next read
*takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a long value,
*a NumberFormatException is thrown.
**********************************************************/
public static long readLong()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Long.parseLong(inputString);
}
/**********************************************************
*Precondition: The next input consists of a double value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhitespace characters
*and returns the double value it represents. Discards the
*first whitespace character after the string read. The next
*read takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a double value,
*a NumberFormatException is thrown.
*********************************************************/
public static double readDouble()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Double.parseDouble(inputString);
}
/*********************************************************
*Precondition: The next input consists of a float value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters and
*returns the float value it represents. Discards the first
*whitespace character after the string read. The next read
*takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a float value,
*a NumberFormatException is thrown.
*********************************************************/
public static float readFloat()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Float.parseFloat(inputString);
}
/**********************************************************
*Reads the first string of nonwhite characters and returns
*that string. Discards the first whitespace character after
*the string read. The next read takes place immediately after
*the discarded whitespace. In particular, if the string
*read is at the end of a line, the next reading will take
*place starting on the next line. Note, that if it receives
*blank lines, it will wait until it gets a nonwhitespace
*character.
*********************************************************/
public static String readWord()
{
String result = "";
char next;
next = readChar();
while (Character.isWhitespace(next))
next = readChar();
while (!(Character.isWhitespace(next)))
{
result = result + next;
next = readChar();
}
if (next == '\r')
{
next = readChar();
if (next != '\n')
{
System.out.println(
"Fatal Error in method readWord of class SavitchIn.");
System.exit(1);
}
}
return result;
}
/*********************************************************
*Precondition: The user has entered a number of type byte on
*a line by itself, except that there may be white space before
*and/or after the number.
*Action: Reads and returns the number as a value of type byte.
*The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
*********************************************************/
public static byte readLineByte()
{
String inputString = null;
byte number = -123;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Byte.parseByte(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be a");
System.out.println("whole number in the range");
System.out.println("-128 to 127, written as");
System.out.println("an ordinary numeral, such as 42.");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
/*********************************************************
*Precondition: The user has entered a number of type short on
*a line by itself, except that there may be white space before
*and/or after the number.
*Action: Reads and returns the number as a value of type short.
*The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
********************************************************/
public static short readLineShort()
{
String inputString = null;
short number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Short.parseShort(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be a");
System.out.println("whole number in the range");
System.out.println("-32768 to 32767, written as");
System.out.println("an ordinary numeral, such as 42.");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
public static byte readByte() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Byte.parseByte(inputString);
}
public static short readShort() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Short.parseShort(inputString);
}
//The following was intentionally not used in the code for
//other methods so that somebody reading the code could more
//quickly see what was being used.
/*********************************************************
*Reads the first byte in the input stream and returns that
*byte as an int. The next read takes place where this one
*left off. This read is the same as System.in.read(),
*except that it catches IOExceptions.
********************************************************/
public static int read()
{
int result = -1; //To keep the compiler happy
try
{
result = System.in.read();
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -