日期:2014-05-20  浏览次数:20616 次

哪里在错了
 
import javax.swing.*;
import java.util.StringTokenizer;
public class WordReplacement {
  String[]word;
  String orignalWords;
  String soWord;
  String deWord;
  String words;
   
  public WordReplacement() {
  word=new String[100];
  orignalWords=JOptionPane.showInputDialog(null,"请输入要修改的文字");
  soWord=JOptionPane.showInputDialog(null,"请输入需要替换的单词");
  deWord=JOptionPane.showInputDialog(null,"请输入替换单词");
  word=splitWords(orignalWords);
  words=replaceWord(word,soWord,deWord);
   
  JOptionPane.showMessageDialog(null,"源文字: "+orignalWords+"\n换成: \n"+words,"替换结果是:",JOptionPane.INFORMATION_MESSAGE);
  }
  public String[]splitWords(String words){
  String[] wordString=words.split(" ");  
  return wordString;
  }
  public String replaceWord(String[] word,String source,String dest){
  String result="";
  for(int i=0;i<word.length;i++){
  if(word[i].compareTo(source)==0){
  word[i]=dest;
  }
  }
  for(int i=0;i<word.length;i++){
  result.concat(word[i]);
  }
  return result;
  }
   
  public static void main(String[] args) {
  WordReplacement wr=new WordReplacement();
  System.exit(0);
   
  }
}



 怎么用words结果为空的,如果words.split(" "); 换成用stringtokenizer分解怎么用,谢谢

------解决方案--------------------
Java code


StringTokenizer st = new StringTokenizer("this is a test");
String[] word=new String[st.countTokens];
int i=0;     
while (st.hasMoreTokens()){
         word[i++]=st.nextToken();
     }