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);
}
}
谢谢!那正是需要做的。 – Scr8789
@ Scr8789不要忘记将答案标记为已接受。 – Vulcan
用于字符串比较的'=='和'.equals()'的业务特别讨厌,因为'=='在大多数情况下工作很长时间,但有时并非如此。很高兴你明白了。 –