格式化输入类,代码中有一些不懂,请高人指点一二

jrx 发布于 2013/05/29 08:54
阅读 175
收藏 0
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());
          }
        }
      }
    }
//不太懂的地方,希望高人能够说详细点,谢谢
加载中
0
铂金小龟
铂金小龟

这个是这样的哈,从你注释中的问题,我觉得,你应该先去看一本书。

叫做《Java编程思想》。

0
王瑞平
王瑞平

 longserialVersionUID = 90001L;

那个LONG值是做序列化用的

当一个需要序列化是,需要一个ID标示。不设置也不会死,没什么大不了

0
王瑞平
王瑞平
StreamTokenizer类自己查下
返回顶部
顶部