I am trying to write a Java program that reads an input file consisting of URLs, extracts tokens from these, and keeps track of how many times each token appears in the 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?
Arrays in Java are fixed length, so I think what you really want to do is use a List<Tokens>
e.g.
List<Tokens> t = new ArrayList<Tokens>();
and
t.add(new Tokens(...))
unless you know in advance the number of items you'll have.
Initialize it:
// Declaration:
Tokens[] t;
// Initialization:
t = new Tokens[10]; // (Or whatever your desired length is)
You can combine declaration and initialization, and many do. I'm not a fan of doing so, but:
Tokens[] t = new Tokens[10];
You'll have the same issue with htokens
.
You may want to look at the List
interface (and its various implementations) instead of using an array.
Your code declares that t will represent an array of Tokens.
However, it does not define that array.
Per the Java Documentation, you need a line like:
t = new Tokens[10]; // Or however large the array should be
You are not initializing Tokens t[];
before using it.
EDIT : You need to it as below :
Tokens[] t = new Tokens[100]; // 100 is just an example
Or use List<Tokens>
.
The modified code : < as per Brian Agnew's answer >
import java.io.*;
import java.net.*;
import java.util.*;
public class Main {
static class Tokens
{
String name;
int count;
Tokens(String str,int c)
{
name=str;
count=c;
}
}
public static void main(String[] args) {
String url_str,host;
String htokens[];
URL url;
boolean found=false;
List<Tokens> t = new ArrayList<Tokens>();
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())
{
found=false;
for(j=0;j<t.size();j++)
{
if(htokens[i].equals(t.get(j).name))
{
k=t.get(j).count+1;
t.set(j,new Tokens(htokens[i],k));
found=true;
break;
}
}
if(!found)
{
t.add(new Tokens(htokens[i],1));
}
}
}
}
System.out.println(t.size() + "class tokens :");
for(i=0;i<t.size();i++)
{
System.out.println("name :"+t.get(i).name+" freq :"+t.get(i).count);
}
br.close();
fr.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Just to mention, you should not use C-like array syntax, i.e. use
String[] names = { "Walter", "Hans", "Bill" };
Instead of
String names[] = { "Walter", "Hans", "Bill" };
精彩评论