我在我的java类中有一个分配来创建一个非常简单的网络钓鱼扫描程序。程序需要读取文本文件,然后为列出的单词指定一个点值,然后打印出单词频率和点值的摘要。
最终结果看起来像这样。计数值将根据单词的频率而变化。
我的问题是,在制作测试字符串以检查值和频率时,它可以正常工作。当我从文本文件中读取并将其转换为数组列表然后转换为数组时,它不会按照它应该的方式工作。 testWords数组具有所有正确的值,但是当我尝试针对网络钓鱼词组进行检查时,它不会注册任何单词。我不完全确定会出现什么问题,因为它似乎应该完美无缺。如果我能得到解释或解决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);
}
}
答案 0 :(得分:1)
这行代码可能没有按照您的想法进行。除非字符串被实习,否则使用==
进行比较与使用.equals()
if(testWords[j] == phishingWords[i])
请尝试使用此代码:
if(testWords[j].equals(phishingWords[i]))
了解String internment here