Question

I have an assignment in my java class to make a very simple phishing scanner. The program needs to read a text file and then assign a point value to the listed words, then print out a summary of the word frequency and point values.

The end result would look something like this. The count value would change based on the frequency of the word.

Results

My issue is that while making a test string to check values and frequency it works fine. When I read from the text file and convert it into an array list then into an array it doesn't work the way it should. The testWords array has all the correct values but when I attempt to check it against the phishingWords array it doesn't register any of the words. I'm not entirely sure what is going wrong because it seems like it should work perfectly fine. If I could get an explanation or a solution to what is going wrong in the wordTest method it would be greatly appreciated.

Here is my code:

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);
}
}
Was it helpful?

Solution

This line of code is probably not doing what you think it is doing. Unless Strings are interned, using == for comparison is not the same as using .equals()

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

Try using this instead:

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

Read about String internment here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top