在我的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);
}
}发布于 2012-12-01 03:30:59
这行代码可能没有做您认为它正在做的事情。除非字符串被实例化,否则使用==进行比较与使用.equals()不同
if(testWords[j] == phishingWords[i])试着使用下面的代码:
if(testWords[j].equals(phishingWords[i]))阅读关于String internment here的文章
https://stackoverflow.com/questions/13651797
复制相似问题