【发布时间】:2018-02-26 19:15:51
【问题描述】:
我正在从文本文件中读取一个单词(程序),并且我想将它存储到我的二维数组中,名为word1。为此,我读入文件并存储到占位符数组中。然后,我将此占位符数组转换为 char 数组,以便拆分每个字母。我现在想将此字符数组中的单个字母发送回我之前创建的字符串数组 (word1)。最终,我希望word1 数组变成这样
String word1[][] = {
{"p", "*"}, {"r", "*"}, {"o", "*"}, {"g", "*"}, {"r", "*"}, {"a", "*"}, {"m", "*"},
};
一切正常,直到我尝试将 char 数组中的单个字母转换回 word1 数组的最后一点。
FileReader file = new FileReader("C:/Users/Mark/Desktop/Java/Workshop 2/hangman.txt");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine(); //Keeps reading line after line
while (line != null){
text += line;
line = reader.readLine();
}
String word1[][] = {
{"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"},
};
String placeholder[] = text.split("\\s+"); //Converts text into an array
String s = "";
for (String n:placeholder){
s+= n;
}
char[] charArray = s.toCharArray();
for (int i = 0; i < 6; i++){
word1[i][0] = new String(charArray[i]); //This is where problem occurs
}
【问题讨论】: