V
Victoria
I want to find out the no of occurence of a keyword(Traditional
Chinese) in a web page (Traditional Chinese).
Now, I can get the web page content and display it.
And, I can get keyword from a test.txt file and display it.
However,I cannot count this keyword occurence in a web page and there
is a null pointer exeption .
How to solve this problem?
Below is the program with java.lang.NullPointerException.
import java.io.*;
import java.net.URL;
public class Check
{
// count the occurence of a keyword in a str (web site content)
public static int Count(String str, String keyword){
int count=0;
int index=str.indexOf(keyword);
while(index!=-1)
{
count++;
index=str.indexOf(keyword,index+1);
}
return count;
}
public static void main(String [] args) throws IOException
{
System.out.println("Please wait...");
// Retrieve first line (keyword) only in test.txt
File f_keyword = new File ( "test.txt" );
FileInputStream FIO = new FileInputStream (f_keyword );
BufferedReader inK = new BufferedReader(new InputStreamReader(
FIO ));
int kLine = 0;
String kline = null ;
while( (kLine < 2) && ((kline = inK.readLine()) != null))
{
kLine++;
System.out.println( kline ); // print the keyword
}
// Retrieve first 10 lines only in the web site
BufferedReader in = new BufferedReader(new InputStreamReader(
new
URL("http://www.yahoo.com.hk/" ).openStream() ));
int iLine = 0;
String line;
String total = null;
while( (iLine < 100) && ((line = in.readLine()) != null))
{
iLine++;
//System.out.println( line );
total += line;
}
System.out.println( total );
// count the occurence of the keyword
int result = Count(total,kline);
System.out.println( "Result : " + result );
}
}
Victoria
Chinese) in a web page (Traditional Chinese).
Now, I can get the web page content and display it.
And, I can get keyword from a test.txt file and display it.
However,I cannot count this keyword occurence in a web page and there
is a null pointer exeption .
How to solve this problem?
Below is the program with java.lang.NullPointerException.
import java.io.*;
import java.net.URL;
public class Check
{
// count the occurence of a keyword in a str (web site content)
public static int Count(String str, String keyword){
int count=0;
int index=str.indexOf(keyword);
while(index!=-1)
{
count++;
index=str.indexOf(keyword,index+1);
}
return count;
}
public static void main(String [] args) throws IOException
{
System.out.println("Please wait...");
// Retrieve first line (keyword) only in test.txt
File f_keyword = new File ( "test.txt" );
FileInputStream FIO = new FileInputStream (f_keyword );
BufferedReader inK = new BufferedReader(new InputStreamReader(
FIO ));
int kLine = 0;
String kline = null ;
while( (kLine < 2) && ((kline = inK.readLine()) != null))
{
kLine++;
System.out.println( kline ); // print the keyword
}
// Retrieve first 10 lines only in the web site
BufferedReader in = new BufferedReader(new InputStreamReader(
new
URL("http://www.yahoo.com.hk/" ).openStream() ));
int iLine = 0;
String line;
String total = null;
while( (iLine < 100) && ((line = in.readLine()) != null))
{
iLine++;
//System.out.println( line );
total += line;
}
System.out.println( total );
// count the occurence of the keyword
int result = Count(total,kline);
System.out.println( "Result : " + result );
}
}
Victoria