import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;class InvalidUserInputException extends Exception {
public InvalidUserInputException() {
super("Invalid user exception");
} public InvalidUserInputException(String message) {
super(message);
}
private static final long serialVersionUID = 90001L; //这里把serialVersionUID设置为90001L有什么用?
}class FormattedInput { public int readInt() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) { //这里readToken代表什么,StreamTokenizer.TT_NUMBER代表什么
throw new InvalidUserInputException("readInt() failed. Input data not numeric.");
} if (tokenizer.nval > (double) Integer.MAX_VALUE //tokenizer.nval代表什么? nval代表什么
|| tokenizer.nval < (double) Integer.MIN_VALUE) {
throw new InvalidUserInputException("readInt() failed. Input outside range of type int.");
} if (tokenizer.nval != (double) (int) tokenizer.nval) {
throw new InvalidUserInputException("readInt() failed. Input not an integer");
}
return (int) tokenizer.nval;
} public double readDouble() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) {
throw new InvalidUserInputException("readDouble() failed. Input data not numeric.");
}
return tokenizer.nval;
} public String readString() throws InvalidUserInputException {
if (readToken() == StreamTokenizer.TT_WORD || ttype == '\"'
|| ttype == '\"') {
return tokenizer.sval;
} else {
throw new InvalidUserInputException("readString() failed. Input data is not a string.");
}
}
private int readToken() { //这个成员函数好像一直都没有被执行到,它 在这里有什么用 ?
try {
ttype = tokenizer.nextToken(); //tokenizer.nextToken()有什么用?
return ttype; } catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return 0;
}
private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //这一句好像也不会被执行?
private int ttype;
}public class TestFormattedInput {
public static void main(String[] args) {
FormattedInput kb = new FormattedInput();
for (int i = 0; i < 5; ++i) {
try {
System.out.print("Enter an integer:");
System.out.println("Integer read:" + kb.readInt());
System.out.print("Enter a double value:");
System.out.println("Double value read:" + kb.readDouble());
System.out.print("Enter a string:");
System.out.println("String read:" + kb.readString());
} catch (InvalidUserInputException e) {
System.out.println("InvalidUserInputException thrown.\n" + e.getMessage());
}
}
}
}请给我详细解释下代码中加注释的部分,我是新手,很多不太懂的地方,希望高人能够说详细点,谢谢CSDN上的好心朋友。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;class InvalidUserInputException extends Exception {
public InvalidUserInputException() {
super("Invalid user exception");
} public InvalidUserInputException(String message) {
super(message);
}
private static final long serialVersionUID = 90001L; //这里把serialVersionUID设置为90001L有什么用?
}class FormattedInput { public int readInt() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) { //这里readToken代表什么,StreamTokenizer.TT_NUMBER代表什么
throw new InvalidUserInputException("readInt() failed. Input data not numeric.");
} if (tokenizer.nval > (double) Integer.MAX_VALUE //tokenizer.nval代表什么? nval代表什么
|| tokenizer.nval < (double) Integer.MIN_VALUE) {
throw new InvalidUserInputException("readInt() failed. Input outside range of type int.");
} if (tokenizer.nval != (double) (int) tokenizer.nval) {
throw new InvalidUserInputException("readInt() failed. Input not an integer");
}
return (int) tokenizer.nval;
} public double readDouble() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) {
throw new InvalidUserInputException("readDouble() failed. Input data not numeric.");
}
return tokenizer.nval;
} public String readString() throws InvalidUserInputException {
if (readToken() == StreamTokenizer.TT_WORD || ttype == '\"'
|| ttype == '\"') {
return tokenizer.sval;
} else {
throw new InvalidUserInputException("readString() failed. Input data is not a string.");
}
}
private int readToken() { //这个成员函数好像一直都没有被执行到,它 在这里有什么用 ?
try {
ttype = tokenizer.nextToken(); //tokenizer.nextToken()有什么用?
return ttype; } catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return 0;
}
private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //这一句好像也不会被执行?
private int ttype;
}public class TestFormattedInput {
public static void main(String[] args) {
FormattedInput kb = new FormattedInput();
for (int i = 0; i < 5; ++i) {
try {
System.out.print("Enter an integer:");
System.out.println("Integer read:" + kb.readInt());
System.out.print("Enter a double value:");
System.out.println("Double value read:" + kb.readDouble());
System.out.print("Enter a string:");
System.out.println("String read:" + kb.readString());
} catch (InvalidUserInputException e) {
System.out.println("InvalidUserInputException thrown.\n" + e.getMessage());
}
}
}
}请给我详细解释下代码中加注释的部分,我是新手,很多不太懂的地方,希望高人能够说详细点,谢谢CSDN上的好心朋友。
if (readToken() != StreamTokenizer.TT_NUMBER) { //这里readToken代表什么,StreamTokenizer.TT_NUMBER代表什么这句的话可以参见如下源码
/**
* A constant indicating that a number token has been read.
*/
public static final int TT_NUMBER = -2;
大致意思是一个常量用来表示读取的是一个数字
这句 if (tokenizer.nval > (double) Integer.MAX_VALUE //tokenizer.nval代表什么? nval代表什么可以参见
/**
* If the current token is a number, this field contains the value
* of that number. The current token is a number when the value of
* the <code>ttype</code> field is <code>TT_NUMBER</code>.
* <p>
* The initial value of this field is 0.0.
*
* @see java.io.StreamTokenizer#TT_NUMBER
* @see java.io.StreamTokenizer#ttype
*/
public double nval;navl表示一当前读取的数;其他的lz可以查源码! private int readToken() { //这个成员函数好像一直都没有被执行到,它 在这里有什么用 ?
try {
ttype = tokenizer.nextToken(); //tokenizer.nextToken()有什么用? 这个在执行readXxx方法时进行的判断不是调用了吗?这个方法是用来判断你输入的内容是那种形式(通过返回不同的数值来表示读取的是数字还是字符等)而这些功能都是通过StreamTokenizer类中的nextToken()方法实现的
更加详细的内容lz可以查看StreamTokenizer类的源码
我也是看这个的,希望楼下的大神指正