我真的想弄清楚這段代碼有什麼問題。 .add()和.toArray()在它們下面有紅線。有沒有其他的方式來寫這兩行?我究竟做錯了什麼?java新手:讀取文件中的單詞搜索代碼。 .add()和.toArray()給我錯誤
package prog3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class WordList {
private String filename;
private Word[] words;
public WordList(String fileName) {
this.filename = fileName;
}
//reads in list of words and stores them internally. line by line
//if method reas list of words successfully, then it returns as true, otherwise false
public boolean readFile() {
try{
BufferedReader read;
read = new BufferedReader(new FileReader(filename));
String nextLine;
while ((nextLine = read.readLine())!= null) {
nextLine = nextLine.trim();
read.add(new Word(nextLine));
}
words = read.toArray(new Word[0]);
}catch(IOException ex){
System.out.println("Caught exception: " +ex.getMessage());
return false;
}
return true;
}
//getList is supposed to return the list of words as an array of class Word. How do I do this???
public Word[] getList() {
}
}
將鼠標懸停在紅線上時出現的錯誤是什麼? – hexafraction
找不到符號符號:方法toArray(Word [])) location:變量讀取BufferedReader的類型 – DanielleElizabeth
您需要調用add('ArrayList,而不是緩衝讀取器。 – hexafraction