class array variable initialization error in java

Posted by trinity on Stack Overflow See other posts from Stack Overflow or by trinity
Published on 2010-03-08T18:01:14Z Indexed on 2010/03/08 18:06 UTC
Read the original article Hit count: 534

Filed under:
|
|
|
|

Hello

I am trying to write a java program that reads an input file consisting of URLs , extracts tokens from these , and accordingly insert into : class Tokens { String name ; int count ; } , where name is the name of every unique token , and count is the frequency of that token in the URLs file..I've written the following code :

import java.io.*;
import java.net.*;

public class Main {


      static class Tokens
        {
            String name;
            int count;
        }


        public static void main(String[] args) {

        String url_str,host;
        String htokens[];
        URL url;
        boolean found=false;
        Tokens t[];
        int i,j,k;

        try
        {
            File f=new File("urlfile.txt");
            FileReader fr=new FileReader(f);
            BufferedReader br=new BufferedReader(fr);


            while((url_str=br.readLine())!=null)
            {
            url=new URL(url_str);

            host=url.getHost();
            htokens=host.split("\\.|\\-|\\_|\\~|[0-9]");

            for(i=0;i<htokens.length;i++)
            {
                if(!htokens[i].isEmpty()) 
                {
                    for(j=0;j<t.length;j++)
                    {
                        if(htokens[i].equals(t[j].name))
                        {   t[j].count++;  found=true;    }
                    }
                    if(!found)
                    {
                        k=t.length;
                        t[k].name=htokens[i];
                        t[k].count=1;
                    }
                }
            }

            System.out.println(t.length + "class tokens :");
            for(i=0;i<t.length;i++)
            {
                System.out.println("name :"+t[i].name+" frequency :"+t[i].count);
            }


            }
            br.close();
            fr.close();

        }
        catch(Exception e)
        {
            System.out.println(e);
        }


    }

}

But when i run it , it says : variable t not initialized.. What should i do to set it right ?

© Stack Overflow or respective owner

Related posts about java

Related posts about class