2012-11-30 146 views
0

我在我的java類中有一個作業,可以製作一個非常簡單的網絡釣魚掃描程序。程序需要讀取一個文本文件,然後爲列出的單詞指定一個點值,然後打印出單詞頻率和點值的摘要。網絡釣魚掃描程序幫助

最終結果看起來像這樣。計數值會根據單詞的頻率而改變。

Results

我的問題是,同時使測試字符串檢查值和頻率正常工作。當我從文本文件中讀取數據並將其轉換爲數組列表時,它將無法按照應有的方式工作。 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); 
} 
} 

回答

1

的這行代碼可能不是做什麼,你認爲它在做什麼。除非字符串被拘留,使用==比較是不一樣的使用.equals()

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

使用這個,而不是嘗試:

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

閱讀有關字符串拘禁here

+0

謝謝!那正是需要做的。 – Scr8789

+0

@ Scr8789不要忘記將答案標記爲已接受。 – Vulcan

+0

用於字符串比較的'=='和'.equals()'的業務特別討厭,因爲'=='在大多數情況下工作很長時間,但有時並非如此。很高興你明白了。 –