我需要一些幫助,無論我試圖把它寫入下面的代碼,它不工作。我希望它從Alice.txt文件中讀取,然後將每個單詞放入數組ordArray中,然後使用其餘的程序來計算每個單詞,每個單詞和每個獨特單詞的每次出現次數。請幫助!如果你能給我一些提示,說明我可以做得更好,或者我應該如何實現將信息寫入文件Opplysning.txt的部分,請不要保持安靜。讀取文件中的每一個字到數組
try {
File skrivFil = new File("Opplysning.txt");
FileWriter fw= new FileWriter(skrivFil);
BufferedWriter bw = new BufferedWriter(fw);
Scanner lesFil = new Scanner("Alice.txt");
int i=0;
int totalOrd=0;
int antUnikeOrd=0;
String[] ordArray = new String[5000];
int[] antallOrd = new int[5000];
String ord = lesFil.next().toLowerCase();
totalOrd++;
boolean ordFraFor=false;
int y=0;
int z=0;
for(i=0; i<ordArray.length; i++) {
if (ord.equals(ordArray[i])) {
antallOrd[i]++;
ordFraFor=true;
}
}
if(ordFraFor=false) {
antUnikeOrd++;
y=0;
boolean ordOpptelling=false;
while(ordOpptelling=false) {
if(ordArray[y] == null) {
ordArray[y] = ord;
antallOrd[y]++;
ordOpptelling=true;
}
y++;
}
}
for(String s: ordArray) {
System.out.println(s);
}
lesFil.close();
} catch (Exception e){
System.out.print(e);
}
}
}
編輯: 嘗試添加該文件,我想它的工作,但我仍然沒有能夠寫入到陣列。我真的很糟糕,這就是爲什麼我必須在下週才能真正使用這些東西...... 無論如何,我試圖將所有單詞添加到arraylist中,我希望它能夠工作。它沒有,它給了我一個nosuchelementexception代替。 代碼的一部分:
File tekstFil = new File ("Alice.txt");
Scanner lesFil = new Scanner(tekstFil);
int i=0;
int totalOrd=0;
int antUnikeOrd=0;
ArrayList<String> liste = new ArrayList<String>();
while (lesFil.hasNext()){
liste.add(lesFil.next());
}
String[] ordArray =liste.toArray(new String[liste.size()]);;
int[] antallOrd = new int[5000];
嚴重縮進的代碼長城。問題描述:它不起作用。你真的幫我們幫助你... –