2016-10-28 17 views
-5

文字列内のすべての単語の繰り返しをカウントするコードを書きたいと思います。単語は文字列として入力された文字で互いに区切ります...なぜ私のコードは機能しませんか? すぐにお答えください!すべての単語の繰り返しをカウントします。

public class repeat { 

public static void main(String[] args) { 
    Scanner ss = new Scanner(System.in); 
    System.out.println("Please write a string:"); 
    String s = ss.nextLine(); 
    System.out.println("Please write a character:"); 
    String w = ss.nextLine(); 
    int i = 0; 
    int j = 0; 
    int k = 0; 
    int y=0; 
    for (i=0 ;i < s.length() ;i++) { 
     for (j = 0; j < w.length(); j++) { 
      if (w.charAt(j) == s.charAt(i) && i!=y && i!=0 && i!=s.length() -1) { 
       k += 1; 
       y=i+1; 
      } 
     } 
    } 
    i = 0; 
    j = 0; 
    y = 0; 
    int r = 0; 
    k++; 
    System.out.println(k); 
    String[] a = new String[k]; 
    for (r=0 ;r < k-1 ;r++) { 
     for (j=1 ;j < s.length() ;j++) { 
      for (i = 1; i < w.length(); i++) { 
       if (w.charAt(i) == s.charAt(j)) { 
        a[r] = s.substring(y, j); 
        y = j+1; 
       } 
      } 
     } 
     System.out.println(a[r]); 
    } 
    a[k-1] = s.substring(y+1,s.length()); 
    i = 0; 
    int[] b = new int[k]; 
    while (i <k) { 
     b[i] = 0; 
     i++; 
    } 
    i = j = 0; 
    while (i < k) { 
     while (j != i && j < k) { 
      if (a[i] == a[j]) { 
       a[j] = null; 
       b[i]++; 
      } 
      j++; 
     } 
     i++; 
    } 
    i = j = 0; 
    while (i < k) { 
     if (a[i] != null) { 
      System.out.println(a[i] + " " + b[i]); 
     } 
     i++; 
    } 
} 
} 
+1

Prototypeでは、1文字の変数名は使用しないでください。物事が密集してアルゴリズム的になると、常にコメントします。あなたはどんなエラーを出していますか? – DejaVuSansMono

+1

入力は何ですか、出力は何ですか?あなたは緊急にデバッグを試みましたか? – SMA

+0

私はちょうど一週間前にJavaを学び始め、私は自分のコードをデバッグする方法を知りません!私は例外エラーを受け取ります –

答えて

0

この問題を解決するには、非常に長いアプローチをとってください。一番簡単なのは正規表現https://docs.oracle.com/javase/tutorial/essential/regex/を使うことです。下記の方法をご覧ください。

public Sentence(String sentanceString) {   
    this.fullSentence = sentanceString; 
    breakStringIntoWords(sentanceString);   
} 

private void breakStringIntoWords(String sentanceString) { 
    String[] wordsInString = sentanceString.split("\\W+"); 
    for (String word : wordsInString) { 
     words.add(new Word(word)); 
    } 
} 

2番目の方法では、文章([spaces]で区切られた)を単語に分割しました。ここから、各単語(文字列メソッドを持つクラスを文字列として扱う)を単語配列リストの他のすべての単語と比較するためのコードを書くと、過度のカウントを避けるように注意してください。

0

(間違った言語私はC#で誤ってそれをやった - Java用の次の回答を参照してください)

私はcharのWiki検索する部分文字列を持つ非常に複雑な作業ですので、このためにアレイとスプリットを使用することをお勧めします。 wはまだ文字列ですが、cは型charである必要があります。

 String[] foundwords = { }; 
     Int32[] wordcount = { }; 

     foreach (String word in s.Split(w)) 
     { 
      int IndexOfWord = Array.IndexOf(foundwords, word); 
      if (IndexOfWord < 0) 
      { 
       Array.Resize(ref foundwords, foundwords.Length + 1); 
       Array.Resize(ref wordcount, wordcount.Length + 1); 
       foundwords[foundwords.GetUpperBound(0)] = word; 
       wordcount[foundwords.GetUpperBound(0)] = 1; 
      } 
      else 
      { 
       wordcount[IndexOfWord]++; 
      } 
     } 
     for (int i = 0; i <= foundwords.GetUpperBound(0); i++) 
     { 
      Console.WriteLine(String.Format("Found word '{0}' {1} times.", foundwords[i], wordcount[i])); 
     } 

大文字と小文字が区別されることに注意してください。

+0

申し訳ありません私は間違ったプログラミング言語(C#)であった - 私はすぐにjavaのために書き直します –

0

okこれは文字列を手動で検索するのではなく、分割されたJavaになっています。 copyarrayがそれを大きくするためのベストプラクティスであるかどうかは正確にはわかりませんが、文字列がメガバイト問題:

public class repeat 
{ 
    public static void main(String[] args) 
    { 
     String s = "Hello world this is a very good test to a world just that contains just more words than just hello"; 
     String w = " "; 

     String[] foundwords = new String[0]; 
     int[] wordcount = new int[0]; 
     String[] splittext = s.split(w); 
     for (int i = 0; i< splittext.length; i++) 
     { 
      int IndexOfWord = getIndexOfWord(splittext[i], foundwords); 
      if (IndexOfWord < 0) 
      { 
       String[] foundwordsTemp = new String[foundwords.length + 1]; 
       int[] wordcountTemp = new int[foundwords.length + 1]; 
       System.arraycopy(foundwords, 0, foundwordsTemp, 0, foundwords.length); 
       System.arraycopy(wordcount, 0, wordcountTemp, 0, foundwords.length); 
       foundwords = new String[foundwords.length + 1]; 
       wordcount = new int[wordcount.length + 1]; 
       System.arraycopy(foundwordsTemp, 0, foundwords, 0, foundwordsTemp.length); 
       System.arraycopy(wordcountTemp, 0, wordcount, 0, foundwordsTemp.length); 
       foundwords[foundwords.length-1] = splittext[i]; 
       wordcount[foundwords.length-1] = 1; 
      } 
      else 
      { 
       wordcount[IndexOfWord]++; 
      } 
     } 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      System.out.println(String.format("Found word '%s' %d times.", foundwords[i], wordcount[i])); 
     } 
    } 
    private static int getIndexOfWord(String word, String[] foundwords) 
    { 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      if (word.equals(foundwords[i])) 
      { 
       return i; 
      } 
     } 
     return -1; 
    } 
} 
+0

あなたの助けを大変ありがとう...しかし、これは私の宿題であり、私は部分文字列でこれを解決する必要があります...あなたはもう一度私を助ける! –

0
あなたのリストにまだ残っている場合は、私はコードを作った。

最初はあなたがどこにいても、メインプロシージャでのみ実行できるようになりました。ストップ・スタート・ストップ・プログラムを書くのではなく、仕事を始めて別の仕事に分けてください。 「良い」名前の関数を使用すると、将来的にはより簡単になります。

まず、別の文字列で文字列を見つける必要があります。

通常は、この関数でJavaのビルドである

int dividerPosition = restString.indexOf(searchString); 

使用することができます。

:あなたはそれを自分で書きたい場合は、それは同じことをするでしょうが、あなたはそれが:)

private static int indexOf(String restString, String searchString) 
{ 
    int dividerPosition = -1; 
    for (int i = 0; i < restString.length()-searchString.length(); i++) 
    { 
     // Debuging test: 
     System.out.println(String.format("search Pos %d in '%s' for length %d.", i, restString, searchString.length())); 
     if (restString.substring(i, i + searchString.length()).equals(searchString)) 
     { 
      dividerPosition = i; 
      i = restString.length(); 
     } 
    } 
    return dividerPosition; 
} 

の作業「を参照してください」などに後のコードでは、この機能を使用することができます(このような関数を作成することができます

int dividerPosition = indexOf(restString, searchString); 

私は再び言葉が出回っ

private static int getIndexOfWord(String word, String[] foundwords) 
{ 
    for (int i = 0; i < foundwords.length; i++) 
    { 
     if (word.equals(foundwords[i])) 
     { 
      return i; 
     } 
    } 
    return -1; 
} 

サードタスを知られているいずれかを見つけるために関数を使用します。 kは分割され、見つかった位置の単語を数えます。私の意見では、見つかった単語を文字列から切り取るだけです - 検索された単語を配列に "保存"する関数を書きます。見つかりました。

この最も重要なことは重要です。検索する文字列の位置を調べるだけです。 変数に見つかった単語(見つかった文字列の前の部分)を保存し、「カウントするか新しい単語を作成する」ということを行います。そして、私たちは単語とSeach-Stringの文字列を返します。

原点の文字列を最初の単語を入れずに置き換え、原点の文字列が ""になるまでこれを繰り返すので、カットオフは重要です。 最後の単語については、dividerPositionをRestStringの長さに変更することで関数が ""を返すことを保証します。これは最後の単語だけです - "searchString.length()"を差し引いて "restString"という戻り値に収まるようにします。サブストリング(dividerPosition + searchString.length()); " (「

あなたがcommentlinesを変更することによって、自己書かれたIndexOfの機能またはJava機能をint型で実行することができます

 /// Index Of Search (better) 
     //int dividerPosition = restString.indexOf(searchString); 

     /// Manual Search (why make it more difficuilt - you should learn to make your work as easy as possible) 
     int dividerPosition = indexOf(restString, searchString); 
でgetNextW「という名前の関数に次の部分で

ルック」」戻ります:今、すべて一緒に - 文字列が空になるまで

すべてが一緒に

は、関数を「カット」を使用して、メインの手順ではほとんどのコードを持っていますstartetを取得しますcharAtとイムと

public class repeat 
{ 
    public static void main(String[] args) 
    { 
     String s = "Hello a world a this is a very good test to a a a a world just that contains just more words than just hello"; 
     String w = " "; 

     while (!(s = getNextW(s, w)).equals("")) 
     { 
      System.out.println(s); 
     } 
     System.out.println(""); 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      // Debuging test: 
      System.out.println(String.format("Found word '%s' %d times.", foundwords[i], wordcount[i])); 
     } 
    } 
    private static String[] foundwords = new String[0]; 
    private static int[] wordcount = new int[0]; 

    private static String getNextW(String restString, String searchString) 
    { 

     /// Index Of Search (better) 
     //int dividerPosition = restString.indexOf(searchString); 

     /// Manual Search (why make it more difficuilt - you should learn to make your work as easy as possible) 
     int dividerPosition = indexOf(restString, searchString); 

     String foundWord; 
     if (dividerPosition > 0) 
     { 
      foundWord = restString.substring(0, dividerPosition); 
     } 
     else 
     { 
      foundWord = restString; 
      dividerPosition = restString.length()-searchString.length(); 
     } 
     int IndexOfWord = getIndexOfWord(foundWord, foundwords); 
     if (IndexOfWord < 0) 
     { 
      String[] foundwordsTemp = new String[foundwords.length + 1]; 
      int[] wordcountTemp = new int[foundwords.length + 1]; 
      System.arraycopy(foundwords, 0, foundwordsTemp, 0, foundwords.length); 
      System.arraycopy(wordcount, 0, wordcountTemp, 0, foundwords.length); 
      foundwords = new String[foundwords.length + 1]; 
      wordcount = new int[wordcount.length + 1]; 
      System.arraycopy(foundwordsTemp, 0, foundwords, 0, foundwordsTemp.length); 
      System.arraycopy(wordcountTemp, 0, wordcount, 0, foundwordsTemp.length); 
      foundwords[foundwords.length-1] = foundWord; 
      wordcount[foundwords.length-1] = 1; 
     } 
     else 
     { 
      wordcount[IndexOfWord]++; 
     } 
     // Debuging test: 
     System.out.println(String.format("Rest of String is '%s' positionnext is %d.", restString, dividerPosition)); 
     return restString.substring(dividerPosition+searchString.length()); 
    } 
    private static int getIndexOfWord(String word, String[] foundwords) 
    { 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      if (word.equals(foundwords[i])) 
      { 
       return i; 
      } 
     } 
     return -1; 
    } 
    private static int indexOf(String restString, String searchString) 
    { 
     int dividerPosition = -1; 
     for (int i = 0; i < restString.length()-searchString.length(); i++) 
     { 
      // Debuging test: 
      System.out.println(String.format("search Pos %d in '%s' for length %d.", i, restString, searchString.length())); 
      if (restString.substring(i, i + searchString.length()).equals(searchString)) 
      { 
       dividerPosition = i; 
       i = restString.length(); 
      } 
     } 
     return dividerPosition; 
    } 
} 

その他の変種は、(潜在的にはるかに大きいの)大きな配列にになりますどのような「配列のサイズに単語を数える」のあなたの種類を使用しています:

public class repeat 
{ 
    private static String[] foundwords; 
    private static int[] wordcount; 
    private static int counter; 
    public static void main(String[] args) { 
     String s = "Hello a world a this is a very good test to a a a a world just that contains just more words than just hello"; 
     String w = " "; 
     int tempPos = 0; 
     counter = 1; // counting total w-strings+1 for dim 
     while ((tempPos = findnext(s, w, tempPos)) >= 0) 
     { 
      tempPos = tempPos + w.length(); 
      counter++; 
     } 
     foundwords = new String[counter]; 
     wordcount = new int[counter]; 
     counter = 0; 
     while ((tempPos = findnext(s, w, 0)) >= 0) 
     { 
      String foundWord = s.substring(0, tempPos); 
      s = s.substring(tempPos + w.length()); 
      foundWordToArray(foundWord); 
     } 
     foundWordToArray(s); 
     for (int i = 0; i < counter; i++) 
     { 
      System.out.println(String.format("Found word '%s' %d times.", foundwords[i], wordcount[i])); 
     } 
    } 
    public static int findnext(String haystack, String needle, int startPos) 
    { 
     int hpos, npos; 
     for (hpos = startPos; hpos < haystack.length()-needle.length(); hpos++) 
     { 
      for (npos = 0; npos < needle.length(); npos++) 
      { 
       if (haystack.charAt(hpos+npos)!=needle.charAt(npos)) 
       { 
        npos = needle.length()+1; 
       } 
      } 
      if (npos == needle.length()) 
      { 
       return hpos; 
      } 
     } 
     return -1; 
    } 
    private static int getIndexOfWord(String word, String[] foundwords) 
    { 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      if (word.equals(foundwords[i])) 
      { 
       return i; 
      } 
     } 
     return -1; 
    } 
    private static void foundWordToArray(String foundWord) 
    { 
     int IndexOfWord = getIndexOfWord(foundWord, foundwords); 
     if (IndexOfWord < 0) 
     { 
      foundwords[counter] = foundWord; 
      wordcount[counter] = 1; 
      counter++; 
     } 
     else 
     { 
      wordcount[IndexOfWord]++; 
     } 
    } 
} 

私は好きこの1つ:

public class repeat 
{ 
    private static String[] foundwords = new String[0]; 
    private static int[] wordcount = new int[0]; 
    public static void main(String[] args) { 
     String s = "Hello a world a this is a very good test to a a a a world just that contains just more words than just hello"; 
     String w = " "; 
     int tempPos; 
     while ((tempPos = findnext(s, w, 0)) >= 0) 
     { 
      String foundWord = s.substring(0, tempPos); 
      s = s.substring(tempPos + w.length()); 
      foundWordToArray(foundWord); 
     } 
     foundWordToArray(s); 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      System.out.println(String.format("Found word '%s' %d times.", foundwords[i], wordcount[i])); 
     } 
    } 
    private static void foundWordToArray(String foundWord) 
    { 
     int IndexOfWord = getIndexOfWord(foundWord, foundwords); 
     if (IndexOfWord < 0) 
     { 
      String[] foundwordsTemp = new String[foundwords.length + 1]; 
      int[] wordcountTemp = new int[foundwords.length + 1]; 
      System.arraycopy(foundwords, 0, foundwordsTemp, 0, foundwords.length); 
      System.arraycopy(wordcount, 0, wordcountTemp, 0, foundwords.length); 
      foundwords = new String[foundwords.length + 1]; 
      wordcount = new int[wordcount.length + 1]; 
      System.arraycopy(foundwordsTemp, 0, foundwords, 0, foundwordsTemp.length); 
      System.arraycopy(wordcountTemp, 0, wordcount, 0, foundwordsTemp.length); 
      foundwords[foundwords.length-1] = foundWord; 
      wordcount[foundwords.length-1] = 1; 
     } 
     else 
     { 
      wordcount[IndexOfWord]++; 
     } 
    } 
    public static int findnext(String haystack, String needle, int startPos) 
    { 
     int hpos, npos; 
     for (hpos = startPos; hpos < haystack.length()-needle.length(); hpos++) 
     { 
      for (npos = 0; npos < needle.length(); npos++) 
      { 
       if (haystack.charAt(hpos+npos)!=needle.charAt(npos)) 
       { 
        npos = needle.length()+1; 
       } 
      } 
      if (npos == needle.length()) 
      { 
       return hpos; 
      } 
     } 
     return -1; 
    } 
    private static int getIndexOfWord(String word, String[] foundwords) 
    { 
     for (int i = 0; i < foundwords.length; i++) 
     { 
      if (word.equals(foundwords[i])) 
      { 
       return i; 
      } 
     } 
     return -1; 
    } 
} 
+0

「charAt」を使用した別のバリアント: –

関連する問題