2016-11-28 15 views
0

私は、テキストファイルからテキストを読み込んで、単語カウントを生成し、ファイル内で使用されている各単語の出現をリストするプログラムを作成する作業をします。Javaのハイフンで文字列を爆発させる方法は?

私はこの文字列 "hello-funny-world"を3つの別個の文字列として見て、それを私の配列リストに格納したいと思っています。これは、単語数から句読点を取り除くことができました。私はこれまでのところ、コードのこの部分、私は問題を抱えて、私はちょうど1つの文字列と見られて「ハロー面白い世界」を取得してい:

while (reader.hasNext()){ 

      String nextword2 = reader.next(); 

      String nextWord3 = nextword2.replaceAll("[^a-zA-Z0-9'-]", ""); 
      String nextWord = nextWord3.replace("-", " "); 

      int apcount = 0; 
      for (int i = 0; i < nextWord.length(); i++){ 
       if (nextWord.charAt(i)== 39){ 
       apcount++; 
      } 
      } 

      int i = nextWord.length() - apcount; 


      if (wordlist.contains(nextWord)){ 
       int index = wordlist.indexOf(nextWord); 
       count.set(index, count.get(index) + 1); 

      } 
      else{ 
       wordlist.add(nextWord); 
       count.add(1); 
       if (i/2 * 2 == i){ 
        wordlisteven.add(nextWord); 
       } 
       else{ 
        wordlistodd.add(nextWord); 
       } 
      } 
+4

'String#split()'を使うだけで、配列 – TheLostMind

答えて

2

これはあなたのために働くことができます....

List<String> items = Arrays.asList("hello-funny-world".split("-"));

+0

が得られます。感謝しています!これをハイフンを含むすべての文字列に適用する方法はありますか? – BradleyS

+1

あなたはそのようなすべての文字列を分割することができます –

0

セパレータを ' - 'として使用していると考えると、

私はまた、あなたは(のためを使用して、この配列を反復処理することができるようになります簡単なスプリット()は、Javaの

String name="this-is-string"; 
String arr[]=name.split("-"); 
System.out.println("Here " +arr.length); 

を使用するためにあなたをお勧めします)ループ

お役に立てれば。

関連する問題