你每次只读一行,再用StreamTokenizer分析不行吗?
可能是System.in没关闭,tokenizer的方法不会返回.

解决方案 »

  1.   

    怎样只读一行啊,我输入的数据就是只有一行啊,怎么把System.in关闭啊,谢谢了!
      

  2.   

    他不动了就给个 Ctrl + Z 然后回车分析 String 推荐使用 Regular Expressions
      

  3.   

    这是我的程序代码,实现功能是对从键盘输入的字符串做词法分析,该字符串包含由逗号间隔的一系列数据项,然后将每个数据项在单独的一行上输出。import java.util.Vector;
    import java.io.*;public class ParseString {
      public static void main(String[] args) {
        char separator = ',';
        StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        tokenizer.resetSyntax();              
        tokenizer.wordChars('\u0000',(char)(separator-1));    // Everything is a word character
        tokenizer.wordChars((char)(separator+1),'\u00ff');    // except for the separator
        tokenizer.whitespaceChars(separator,separator);       // Whitespace separates words so just the separator
      
        int type = 0;                                         // Stores the value returned by nextToken()
        Vector tokens = new Vector();                         // Will store the tokens that we find
        try {
          while((type = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {   // As long as we don't have EOF
            if(type == StreamTokenizer.TT_WORD)                               // Check for a word
              tokens.add(tokenizer.sval);                                     // and save it in the vector
           // else
              // assert false;                                                   // We only expect words
          }
        }
        catch(IOException e) {
          e.printStackTrace();
          System.exit(1);
        }  System.out.println("The tokens in the string are:"); 
        for( int i = 0 ; i< tokens.size() ; i++)
          System.out.print("  "+(String)tokens.elementAt(i));
      }
    }
      

  4.   

    代码重新发一下,上面的有点乱了
    import java.util.Vector;
    import java.io.*;public class ParseString 
     {
        public static void main(String[] args) 
         {
           char separator = ',';
           StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new     InputStreamReader(System.in)));
           
           tokenizer.resetSyntax();              
           tokenizer.wordChars('\u0000',(char)(separator-1));    
           tokenizer.wordChars((char)(separator+1),'\u00ff');    
           tokenizer.whitespaceChars(separator,separator);       
      
           int type = 0;                                         
           Vector tokens = new Vector();                         
           try 
             {
               while((type = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) 
                 {   
                    if(type == StreamTokenizer.TT_WORD)                               
                    tokens.add(tokenizer.sval);                                    
                    // else
                   // assert false;                                                   
                 }
             }
                 catch(IOException e) 
                 {
                   e.printStackTrace();
                   System.exit(1);
                 }           System.out.println("The tokens in the string are:"); 
               for( int i = 0 ; i< tokens.size() ; i++)
               System.out.print("  "+(String)tokens.elementAt(i));
        }
    }
      

  5.   

    不知道你是不是想把输入按','分开,如果是的话,可以:
    import java.util.*;
    import java.io.*;public class Test
    {
    public static void main(String[] args) throws Exception
    {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter 'quit' to stop.");
    String line = null;
    Vector tokens = new Vector();
    while (true) {
    line = in.readLine();
    if (line.equalsIgnoreCase("quit")) {
    break;
    }
    StringTokenizer token = new StringTokenizer(line,",");
    while (token.hasMoreTokens()) {
    tokens.add(token.nextToken());
    }
    }
    System.out.println("The tokens in the string are:");
    for( int i = 0 ; i< tokens.size() ; i++)
    System.out.print(" "+(String)tokens.elementAt(i));
    System.out.println();
    }
    }