StreamTokenizer预期结束字符

时间:2014-02-23 00:27:53

标签: java

我正在尝试实现一个标记生成器来将数学输入字符串解析为它们各自的部分,并且我在第二行代码中得到的似乎是一个微不足道的错误,因为我发布了它预期的一个;在途中途的某个地方。

public static void parseFormula(String text){
    List<String> tokenize(String s) throws IOException {
          StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
          tokenizer.ordinaryChar('-'); 
          List<String> tokBuf = new ArrayList<String>();
          while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            switch(tokenizer.ttype) {
              case StreamTokenizer.TT_NUMBER:
                tokBuf.add(String.valueOf(tokenizer.nval));
                break;
              case StreamTokenizer.TT_WORD:
                tokBuf.add(tokenizer.sval);
                break;
              default: 
                tokBuf.add(String.valueOf((char) tokenizer.ttype));
            }
          }
          return tokBuf; 
        }
}

2 个答案:

答案 0 :(得分:1)

您有tokenize嵌套在parseFormula中的方法。 Java不允许嵌套方法。这就是编译错误的原因。将这些方法拆开后,以下编译好了:

List<String> tokenize(String s) throws IOException {
    StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
    tokenizer.ordinaryChar('-'); 
    List<String> tokBuf = new ArrayList<String>();
    while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
      switch(tokenizer.ttype) {
        case StreamTokenizer.TT_NUMBER:
          tokBuf.add(String.valueOf(tokenizer.nval));
          break;
        case StreamTokenizer.TT_WORD:
          tokBuf.add(tokenizer.sval);
          break;
        default: 
          tokBuf.add(String.valueOf((char) tokenizer.ttype));
      }
    }
    return tokBuf; 
  }

public static void parseFormula(String text){

}

答案 1 :(得分:-1)

try {
List<String> tokenize = new ArrayList<String>(s);

      StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
      tokenizer.ordinaryChar('-'); 
      List<String> tokBuf = new ArrayList<String>();
      while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
        switch(tokenizer.ttype) {
          case StreamTokenizer.TT_NUMBER:
            tokBuf.add(String.valueOf(tokenizer.nval));
            break;
          case StreamTokenizer.TT_WORD:
            tokBuf.add(tokenizer.sval);
            break;
          default: 
            tokBuf.add(String.valueOf((char) tokenizer.ttype));
        }
      }


}catch (IOException e){
  System.out.println(e);
}

如果我没弄错的话