2017-04-20 10 views
2

私はこのコードをまとめましたが、ちょっとした助けもありました。そのポイントは、ユーザーのメッセージを受け取り、メッセージ内の単語がテキストファイルにも見つかったかどうかを確認することです(約50の一般的な英語の単語)。私の問題は、メッセージ内の各単語の出現も数えたいと思うことです。 intカウンタを追加しようとしましたが、文字列オブジェクトを整数で数えることができないため、エラーが発生しています。私はちょうどにカウンターを追加する場合:ハッシュマップで単語を数える方法

for (String userInput : message.split(" ")) 
{ 
    if (legalEnglishWords.contains(userInput)) 
    { 
     System.out.println(userInput + " is an English word "); 
     count++; 
     System.out.println(userInput + " occurs " + counter + " times"); 
    } 
} 

私はあなたがカウントはされ見ることができるように

Please enter in a message: 
transformers are is is this 

transformers occurs 0 times 
are is an English word 
are occurs 1 times 
is is an English word 
is occurs 2 times 
is is an English word 
is occurs 3 times 
this is an English word 
this occurs 4 times 

のように見える出力を私に残し、法的な単語テストに合格するすべての単語のための反復を取得します全く間違っていて、メッセージに何度も出てくる言葉を表示します。私はそれを一度表示するのが好きです。私は& &を上記のif文に入れて、単語がすでに印刷されているかどうかを確認しますが、何を置くべきかはわかりません。また、ifステートメントが入力されるたびにカウントする代わりに、ifステートメントから個々の単語を数える方法もわかりません。だから、基本的に私は個々の言葉を数える助けを必要とし、 "英語"の言葉は一度しか表示せず、終わりに何回も出現した。私はまだJavaに少し新しく、私のプログラムで起こっていることとその理由をすべて理解しています。可能であれば、私は、潜在的に理解していないもので全体的な見直しの代わりに、必要なことをするためにこのコードを試してみたいと思います。すべてありがとう!

import java.io.*; 
import java.util.HashSet; 
import java.util.Scanner; 

public class Affine_English3 
{  
     public static void main(String[] args) throws IOException 
     { 
       HashSet<String> legalEnglishWords = new HashSet<String>(); 
       Scanner file = new Scanner(new File("example.txt")); 
       int counter = 0; 

       while (file.hasNextLine()) 
       { 
        String line = file.nextLine(); 

       for (String word : line.split(" ")) 
       { 
        { 
         legalEnglishWords.add(word); 
        } 
       } 
       } 

       file.close(); 

       Scanner scan = new Scanner(System.in); 
       System.out.println("Please enter in a message: "); 
       String message = scan.nextLine(); 
       scan.close(); 

       for (String userInput : message.split(" ")) 
       { 
        if (legalEnglishWords.contains(userInput)) 
        { 
         System.out.println(userInput + " is an English word "); 
         counter++; 
        } 
        System.out.println(userInput + " occurs " + counter + " times"); 
       } 
     } 
} 

これは、あなたが正しい道の上にあるが、あなたは、データを格納するコレクションを変更する必要があります私の「一般的な英語の単語」テキストファイルのコピー

the he at but there of was be not use and for can 
a on have all each to are from where which in by 
is with line when do you his had your how that they 
it i word said if this what an as or we she their 
+0

の代わりに含まれています。これで問題は解決します。 –

+1

'Map ' 'String'は' legalEnglishWord'を持ち、 'Integer'は数えます –

+1

@Learner' legalEnglishWords'は文字列の集合です。それは決して単一の文字列と等しいことはありません。 –

答えて

3

です。あなたが各単語のカウントをしたい場合は、JavaではMapが必要です。マップは、一連のキーを一連の値にマップします。名前のマッピングなどです。あなたの場合、単語をカウントにマップする必要があります。

Map<String, Integer> wordCounts = new HashMap<>(); 

次に、あなたが行うことができます単語全体で何か来るたびに:あなたは、Java 8を使用している場合

if (!wordCounts.containsKey(word)) 
    wordCounts.put(word, 1); 
else 
    wordCounts.put(word, wordCounts.get(word) + 1); 

は(間違いなくがある

のでマップは次のように宣言で始まります)より洗練された構文:

wordCounts.compute(word, n -> n == null ? 1 : n + 1); 

すべてのテキストを処理した後、 E数:

for (String word: wordCounts.keySet()) { 
    System.out.println("Word " + word + " occurred " + wordCounts.get(word) + " times"; 
} 
+0

私は自分のコードに少し更新しました – Nolan

+0

Downvote代わりに悪い操作によってupvote。 – davidxxx

+0

@davidxxxあなたのdownvoteをもう少し説明できますか?私が見ることができる限り、あなたはあなたの編集に「あなたが」持っていたものを変更しました(16世紀(https://www.merriam-webster.com/dictionary/)以来の縮小にもかかわらず)あなた)) – sprinter

1

何をしようとするが(EXAMPLE.SQLの中で定義された)各法的な英語の単語の数を追跡することです。だからSetの代わりにMapが必要です。擬似コードで

、それは次のようになります。あなたはlegalEnglishWords.equals(ユーザ入力)を使用する必要があり

Map<String, Integer> legalEnglishWord = new HashMap<>(); 
for each word in "exmample.txt" { 
    legalEnglishWords.put(word, 0); 
} 

// handle input 
for each word in inputMessage { 
    if (legalEnglishWords.containsKey(word)) { 
     legalEnglishWords.put(word, legalEnglishWord.get(word) + 1); 
    } 
    // or simply 
    legalEnglishWords.computeIfPresent(word, count -> return count+1); 
} 

// At this point, you have each legal words, and the appearances of 
// each legal word in the input message. 
関連する問題