txt 读取每一行文字存为一个string
Vector<String> result=null ;
public delStopWord() {
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"data\\stopList.txt"));
while ((line = reader.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean IsStopWord(String word)
{
String sresult=result.toString();//出错 静态变量不能从非静态变量中读取。
String[] stopWordsList=sresult.split("");
for(int i=0;i<stopWordsList.length;i++)
{
if(word.equalsIgnoreCase(stopWordsList[i]))
return true;
}
return false;
}
public String[] DropStopWords(String[] oldWords)
{
Vector<String> v1= new Vector<String>();
for(int i=0;i<oldWords.length;++i)
{
if(IsStopWord(oldWords[i])==false)
{
v1.add(oldWords[i]);
}
}
String[] newWords=new String[v1.size()];
v1.toArray(newWords);
return newWords;
}
Vector<String> result=null ;
public delStopWord() {
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"data\\stopList.txt"));
while ((line = reader.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean IsStopWord(String word)
{
String sresult=result.toString();//出错 静态变量不能从非静态变量中读取。
String[] stopWordsList=sresult.split("");
for(int i=0;i<stopWordsList.length;i++)
{
if(word.equalsIgnoreCase(stopWordsList[i]))
return true;
}
return false;
}
public String[] DropStopWords(String[] oldWords)
{
Vector<String> v1= new Vector<String>();
for(int i=0;i<oldWords.length;++i)
{
if(IsStopWord(oldWords[i])==false)
{
v1.add(oldWords[i]);
}
}
String[] newWords=new String[v1.size()];
v1.toArray(newWords);
return newWords;
}
你这是静态的方法
import java.io.*;
import java.util.Vector;public class txt_string
{
static Vector<String> result=null;
public txt_string()
{
result=new Vector<String>();
}
public void delStopWord() {
String line = null;
try
{
BufferedReader reader = new BufferedReader(new FileReader(
"d:/stopList.txt"));
while ((line = reader.readLine()) != null)
{
result.add(line);
}
}
catch (IOException e)
{
e.printStackTrace();
} }
public static boolean IsStopWord(String word)
{
String sresult=result.toString();//出错 静态变量不能从非静态变量中读取。
// String[] stopWordsList=sresult.split(""); 你这个split用的有问题,而且Vector的toString()未必满足我们的要求,所以还是自己动手
//应该这样:
String[] stopWordsList=new String[result.size()];
for(int i=0;i<result.size();i++)
{
stopWordsList[i]=result.get(i);
}
for(int i=0;i <stopWordsList.length;i++)
{
if(word.equalsIgnoreCase(stopWordsList[i]))
return true;
}
return false;
}
public String[] DropStopWords(String[] oldWords)
{
Vector <String> v1= new Vector <String>();
for(int i=0;i <oldWords.length;++i)
{
if(IsStopWord(oldWords[i])==false)
{
v1.add(oldWords[i]);
}
}
String[] newWords=new String[v1.size()];
v1.toArray(newWords);
return newWords;
}
public static void main(String arg[])
{
txt_string culItem=new txt_string();
culItem.delStopWord();
System.out.print(IsStopWord("w"));
}
}