我正在编写一个程序,我必须从文本文件中读取字符串,并使用带有此代码的扫描程序完成:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
File file = new File(jTextField1.getText());
c = new crows_word_game();
ArrayList<String> list = new ArrayList<>();
try {
Scanner fileIn = new Scanner(file);
String line;
while(fileIn.hasNextLine()) {
line = fileIn.nextLine();
list.add(line);
}
c.setWords((String[]) list.toArray(),c);
} catch (FileNotFoundException ex) {
Logger.getLogger(readfile.class.getName()).log(Level.SEVERE, null, ex);
}
}
我必须将我的数组存储在一个有数组构建的对象中但是当我这样做时它会给我一个错误说:
无法将对象强制转换为字符串
我的方法和类位于
之下class crows_word_game {
public static puzzle_diagram shape;
public static int number;
public static String[] words;
public static int x,y,max;
public static boolean rock=false;
public crows_word_game(){ }
public crows_word_game(puzzle_diagram p,int wordsnum){
shape = p;
number = wordsnum;
//words = new String[number];
//words = file;
x=0;
y=0;
max=0;
}
public void setWords(String[] word,crows_word_game c) {
for(int i=0;i<c.number;i++)
c.words[i] = word[i];
}
}
你能告诉我什么是错的,我能做些什么来解决它