0

非常に単純なフィッシングスキャナーを作成するために、Javaクラスに割り当てがあります。プログラムは、テキストファイルを読み取り、リストされた単語にポイント値を割り当ててから、単語の頻度とポイント値の概要を出力する必要があります。

最終結果は次のようになります。カウント値は、単語の頻度に基づいて変化します。

結果

私の問題は、値と頻度をチェックするためのテスト文字列を作成している間、それが正常に機能することです。テキストファイルを読み取って配列リストに変換してから配列に変換すると、正常に機能しません。testWords配列にはすべて正しい値がありますが、phishingWords配列に対してチェックしようとすると、どの単語も登録されません。何がうまくいかないのか完全にはわかりません。完全に正常に機能するはずだからです。wordTestメソッドで何が問題になっているのかについての説明や解決策を得ることができれば、非常にありがたいです。

これが私のコードです:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;

public class PhishingScanner
{       
private static final int phishingWordsCount[] = new int [30];

private static final String[] phishingWords = {
    "amazon", "official", "bank", "security", "urgent", "alert",
    "important", "information", "ebay", "password", "credit", "verify",
    "confirm", "account", "bill", "immediately", "address", "telephone",
    "ssn", "charity", "check", "secure", "personal", "confidential",
    "atm", "warning", "fraud", "citibank", "irs", "paypal" };

private static final int phishingPoints[] = { 2, 2, 1, 1, 1, 1, 1, 2,
 3, 3, 3, 1, 1, 1, 1, 1, 2, 2, 3, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1 };

//String used for testing the wordTest()
//private static String[] testWords = {"thanks", "amazon", "paypal", "bank", "amazon"};

public static void main(String[] args)
{
    readFile();

    //used for testing the wordTest() not used in final application
    //wordTest(testWords);
}

public static void wordTest(String[] testWords)
{        
    int total = 0;

    for(int j = 0; j < testWords.length; j++)
    {        
        for(int i = 0; i < phishingWords.length; i++)
        {
            if(testWords[j] == phishingWords[i])
            {  
                ++phishingWordsCount[i];

                total += phishingPoints[i];
            }                               
        }
    }

    System.out.printf("%-15s%-10s%s\n","Word", "Count", "Points\n");

    for (int k = 0; k < phishingWords.length; k++)
    {
        System.out.printf("%-15s%-10s%s\n", phishingWords[k] , phishingWordsCount[k], phishingPoints[k]);
    }

    System.out.println("Total points: " + total);
}

private static void readFile() 
{
    ArrayList<String> textFileWords = new ArrayList<String>();

    try
    {
        BufferedReader br = new BufferedReader(new FileReader("c:\\test.txt"));
        String str = "";
        String st;
        while ((st = br.readLine()) != null) 
        {
            str += st + " ";
        }
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        str = str.toLowerCase();
        //^^ reads and converts the entire file into a single lowercase string
        int count = -1;
            for (int i = 0; i < str.length(); i++) 
            {
                if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) 
                {
                    if (i - count > 1) 
                    {
                        if (Character.isLetter(str.charAt(i))) 
                        {
                            i++;
                        }
                        String word = str.substring(count + 1, i);

                        if (map.containsKey(word)) 
                        {
                            map.put(word, map.get(word) + 1);
                        } 
                        else 
                        {
                            map.put(word, 1); 
                        }                                                        
                        textFileWords.add(word);

                        //^^ Reads each word and puts it into the textFileWords Array List
                    }
                    count = i;
                }
            }                
    }       
    catch (Exception e)
    {
        System.out.println(e);
    }       

    String[] testWords = new String[textFileWords.size()];
    testWords = textFileWords.toArray(testWords);

    wordTest(testWords);
}
}
4

1 に答える 1

1

このコード行は、おそらくあなたが思っていることをしていないでしょう。文字列がインターンされていない限り==、比較に使用することは使用することと同じではありません.equals()

 if(testWords[j] == phishingWords[i])

代わりにこれを使用してみてください:

 if(testWords[j].equals(phishingWords[i]))

ここで文字列の抑留について読む

于 2012-11-30T19:30:58.790 に答える