给你一个例子:
import java.util.*;
public class E
{ public static void main(String args[])
  { String s1="I am Geng.X.y,she is my girlfriend";
 String s2="Lowood?what is that?";
 StringTokenizer fenxi_1=new StringTokenizer(s1," ,");//空格和逗号做分隔符。
 StringTokenizer fenxi_2=new StringTokenizer(s2," ?");//问号和空格做分隔符。
 int n1=fenxi_1.countTokens(),n2=fenxi_2.countTokens();
while(fenxi_1.hasMoreTokens())
  { String s=fenxi_1.nextToken();
    System.out.println(s);
  }
    System.out.println("s1有单词:"+n1+"个");
   while(fenxi_2.hasMoreTokens())
     { String s=fenxi_2.nextToken();
       System.out.println(s);
     }
  System.out.println("s2有单词:"+n2+"个");
  } 
}

解决方案 »

  1.   

    //怎么获取从键盘输入的字符窜??
    import java.io.* ;
    import java.util.StringTokenizer;
    import java.util .*;
    import com.newman.lang.MyString ;/**
     * Formula template
     * written be Newman 
     *//**
     * Change hint:
     * m_szHelp,execute(),parseInputString()
     * 
     * commands:
     * 1.quit quit
     * 2.? help
     * 3.
     */
    public class Formula{
    /**
     * information
     */
    public String m_szAuthor="Newman";
    public String m_szVersion="1.0";
    public String m_szProgramName="Formula the World";
    public String m_szContactBy="Email: [email protected]";
    public String m_szHelp="          ***** Help Contents *****\n"+
       "Notice:\n"+
       "1)Press Quit is out.\n"+
       "2)Press ? for help.\n";
    //"3.Please  input number(seperated be Blankspace): .\n"+
    //"4.Example: .";


    /**
     * initial variables
     */
    DataInputStream m_dis=null;
    String m_ReadLine="";

    /**
     * status
     */
    boolean m_bQuit=false;


    /**
     * costructor function
     */
    public Formula(){
    this.m_dis=new DataInputStream(System.in);
    }
    /**
     * parse input strings
     * return false :the data can deal .
     */
    public boolean parseInputString(String str){
    if(str.startsWith("?")){
    this.PRINT(this.getDescription());
    this.PRINT(this.getHelp()); //get help
    }
    else if(str.toLowerCase().startsWith("quit")){//quit
    this.m_bQuit=true ;
    }
    return false;
    }

    /**
     * 初始化程序
     */
    public void init(){
    this.PRINT(this.getDescription());
    while(!this.m_bQuit){
    //this.PRINT(this.getHelp());
    execute();
    }
    }

    /**
     * 执行处理部分
     */
    public boolean execute(){
    if(!this.m_ReadLine .equals ("?")){
    this.PRINT(this.getHelp());
    }
    this.getInputString();

    if(this.parseInputString(this.m_ReadLine)){
    return false;//解析通过
    }else{
    return true;
    }

    /**
     * todo:doing sth.
     */
     

    }


    /**
     * get initial Description
     */
    public String getDescription(){
    String Description="";
    Description="<< "+this.m_szProgramName +" >>";
    Description=Description+"Version "+this.m_szVersion+"\n";
    Description=Description+"Programmed by "+this.m_szAuthor+"\n";
    Description=Description+"Contact by "+this.m_szContactBy+"\n";
    return Description;
    }

    /**
     * get a string token in vector from a string
     */
    public Vector getStringToken(String string,String spiltstring){
    Vector v=new Vector ();
    StringTokenizer st;
    st=new StringTokenizer(string,spiltstring);
    while(st.hasMoreElements()){
    v.addElement(st.nextElement());
    }
    return v;
    }

    /**
     * get the string input on the screen
     */
    public String getInputString(){
    try{
    this.m_ReadLine=this.m_dis.readLine();
    }
    catch(IOException e){
    System.err.println("Error: Can't get system inputstream!");
    }
    return this.m_ReadLine;
    }

    /**
     * get help strings
     */
    public String getHelp(){
    return this.m_szHelp; 
    }

    /**
     * print on the screen
     */
    public void PRINT(String message){
    System.out.println(message);
    }


    /**
    * check the string whether it is pure number
    */
    public boolean isPureNumber(String strSourse){
    String str=strSourse;
    str=str.replace(' ','0');
    str=str.replace('.','0');
    boolean bnum=this.isNumber(str); 
    return bnum;
    } /**
     * return whether the str consists of  pure numbers
     */
    private boolean isNumber(String str){
    int i=0;
    char ch;
    boolean flag=true;
    while((flag)&&(i<str.length())){
    ch=str.charAt (i);
    if(!Character.isDigit(ch)){
    flag=false;
    }
    i++;
    }
    return flag;   
    } public static void main(String[] args)throws IOException{
    Formula f=new Formula();
    f.init();
    }}
      

  2.   

    如果你是用JDK1.4,最简单的:
             String s = "a-s-d-f";
             String element[] = s.split("-");
             System.out.print(Arrays.asList(element));
      

  3.   

    如果会用正则表达式,用java.util.regex 1.4JDK有~
    也可以到apache网站下载