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 回答