package p2p;
import java.io.*;
import java.util.*;
/**
* Class for simple console input. A class designed primarily for simple
* keyboard input of the form one input value per line. If the user enters an
* improper input, i.e., an input of the wrong type or a blank line, then the
* user is prompted to reenter the input and given a brief explanation of what
* is required. Also includes some additional methods to input single numbers,
* words, and characters, without going to the next line.
*/
public class SavitchIn {
/**
* Reads a line of text and returns that line as a String value. The end of
* a line must be indicated either by a new-line character '\n' or by a
* carriage return '\r' followed by a new-line character '\n'. (Almost all
* systems do this automatically. So you need not worry about this detail.)
* Neither the '\n', nor the '\r' if present, are part of the string
* returned. This will read the rest of a line if the line is already
* partially read.
*/
public static String readLine() {
char nextChar;
String result = "";
boolean done = false;
while (!done) {
nextChar = readChar();
if (nextChar == '\n')
done = true;
else if (nextChar == '\r') {
// Do nothing.
// Next loop iteration will detect '\n'.
} else
result = result + nextChar;
}
return result;
}
/**
* Reads the first string of nonwhitespace characters on a line and returns
* that string. The rest of the line is discarded. If the line contains only
* whitespace, the user is asked to reenter the line.
*/
public static String readLineWord() {
String inputString = null, result = null;
boolean done = false;
while (!done) {
inputString = readLine();
StringTokenizer wordSource = new StringTokenizer(inputString);
if (wordSource.hasMoreTokens()) {
result = wordSource.nextToken();
done = true;
} else {
System.out
.println("Your input is not correct. Your input must");
System.out
.println("contain at least one nonwhitespace character.");
System.out.println("Please try again. Enter input:");
}
}
return result;
}
/**
* Precondition: The user has entered a number of type int on a line by
* itself, except that there may be whitespace before and/or after the
* number. Action: Reads and returns the number as a value of type int. 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 int readLineInt() {
String inputString = null;
int 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 = Integer.parseInt(inputString);
done = true;
} catch (NumberFormatException e) {
System.out.println("Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("a whole number written as an");
System.out.println("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 long on a line by
* itself, except that there may be whitespace before and/or after the
* number. Action: Reads and returns the number as a value of type long. 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 long readLineLong() {
String inputString = null;
long 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 = Long.parseLong(inputString);
done = true;
} catch (NumberFormatException e) {
System.out.println("Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("a whole number written as an");
System.out.println("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 double on a line by
* itself, except that there may be whitespace before and/or after the
* number. Action: Reads and returns the number as a value of type double.
* 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 double readLineDouble() {
String inputString = null;
double 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 = Double.parseDouble(inputString);
done = true;
} catch (NumberFormatException e) {
System.out.println("Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("an ordinary number either with");
System.out.println("or without a decimal point,");
System.out.println("such as 42 or 9.99");
System.out.println("Please try again.");
System.out.println("Enter the number:");
}
}
return number;
}
/**
* Precondition: The user has entered a number of type float on a line by
* itself, except that there may be whitespace before and/or after the
* number. Action: Reads and returns the number as a value of type float.
* 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 float readLineFloat() {
String inputString = null;
float 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 = Float.parseFloat(inputString);
done = true;
} catch (NumberFormatException e) {
System.out.println("Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("an ordinary number either with");
System.out.println("or without a decimal point,");
System.out.println("such as 42 or 9.99");
System.out.println("Please try again.");
System.out.println("Enter the number:");
}
}
return number;
}
/**
* Reads the first nonwhitespace character on a line and returns that
* character. The rest of the line is discarded. If the line contains only
* whitespace, the user is asked to reenter the line.
*/
public static char readLineNonwhiteChar() {
boolean done = false;
String inputString = null;
char nonWhite = ' ';// To keep the compiler happy.
while (!done) {
inputString = readLine();
inputString = inputString.trim();
if (inputString.length() == 0) {
System.out.println("Your input is not correct.");
System.out.println("Your input must contain at");
System.out.println("le