find words in a hashset or treeset?

Posted by icelated on Stack Overflow See other posts from Stack Overflow or by icelated
Published on 2010-01-11T02:07:11Z Indexed on 2010/03/29 16:23 UTC
Read the original article Hit count: 425

Filed under:

I am piping in a file and storing it into a treeset. I am trying to count unique words.. I am placing words that i dont want into a hashset. "a","the", "and"

I want to check to see if the file contains those words, before i place them into the TreeSet.. i know i need some sort of if(word == find) ? i just dont know how to do it..

Sorry about formatting. its hard to get it correct after you paste.

this is what i have..

import java.util.Scanner;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.HashSet;

public class Project1
{
    public static void main(String[] args)
    {
        Scanner     sc = new Scanner(System.in);    
        String      word;
        String grab;
        int count = 0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        int number;
        TreeSet<String> a = new TreeSet<String>();
        HashSet<String> find = new HashSet<String>();

        System.out.println("Project 1\n");
        find.add("a");
        find.add("and");
        find.add("the");

        while (sc.hasNext()) 
        {   
            word = sc.next();
            word = word.toLowerCase();
            for(int i = 0; i < word.length(); i++ )
            {
                if(Character.isDigit(word.charAt(i))) 
                {
                    count3++;  
                }
            }
            //if( a.contains("a") )
            //|| word.matches("and") || word.matches("the")|| word.contains("$"))
            //{
            //   count2++;
            // }
            a.add(word);
            if (word.equals("---"))
            {
                break;
            }
        }

        System.out.println("a size");
        System.out.println(a.size());
        // count = count2 - count;
        System.out.println("unique words");
        System.out.println(a.size() -  count2 - count3);
        System.out.println("\nbye...");
    }
}

© Stack Overflow or respective owner

Related posts about java