是用來統計出現的詞語的數量的程序,第一個文件是一些要統計的詞語,第二個是一個空文件,暫時沒啥用。
import java.io.*;
import java.util.*;
public class cotest {
public static void main(String args[]){
File readFile=new File("F:/code/coword.txt"),
writeFile= new File("F:/code/hello.txt");
try{
FileReader inOne=new FileReader(readFile);
BufferedReader inTwo=new BufferedReader(inOne);
FileWriter tofile=new FileWriter("hello.txt");
BufferedWriter out=new BufferedWriter(tofile);
String s=null;
int n;
String a=new String();
str_nu new_word=new str_nu();
LinkedList words=new LinkedList();
while((s=inTwo.readLine())!=null){
Scanner sc=new Scanner(s);
while(sc.hasNext()){
a=sc.next();
//System.out.println(a);
n=getIndex(words,a);
//System.out.println(n);
if(n!=-1){
words.get(n).num++;
System.out.println(words.get(n).num);
}
else{
new_word.word=a;
new_word.num=1;
words.add(new_word);
}
}
}
for(int i=0;i<words.size();i++){
System.out.println("單詞:"+words.get(i).word+" "+words.get(i).num);
if(words.get(i).word=="ccc")
System.out.println("here");
}
if(words.get(1).word=="ccc")
System.out.println("abcd");
inOne.close();
inTwo.close();
}
catch(IOException e){
System.out.println(e);
}
}
private static int getIndex(LinkedList<str_nu> words, String a) {
// TODO Auto-generated method stub
for(int i=0;i<words.size();i++){
if(words.get(i).word==a){
System.out.println(i);
return i;
}
}
return -1;
}
}
class str_nu{
String word=new String();
int num;
}
字符串精確比較要使用String類的equals方法,不能使用 ==
if(words.get(i).word=="ccc")
if(words.get(i).word==a){