1
我想加載文件調用,Tut16_ReadText.txt並使其運行通過程序輸出,如果它的重或輕。需要幫助加載到一個java程序文件
我得到了我粘貼在下面的錯誤。我無法讓這個程序工作。任何人都可以解釋我必須做什麼來使這個計劃的工作?
謝謝
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class test1 {
public static void main(String args[]) throws Exception {
if (args.length != 1) {
System.out.println("usage: Tut16_ReadText file1");
}
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("F:/Programming/Java/Week16/Tut16_ReadText.txt"));
String sCurrentLine;
int totalwords = 0, totalchar = 0;
while ((sCurrentLine = br.readLine()) != null) {
String words[] = sCurrentLine.split(" ");
totalwords += words.length;
for (int j = 0; j < words.length; j++) {
totalchar += words[j].length();
}
}
double density = (1.0 * totalchar)/totalwords;
if (totalchar > 0) {
System.out.print(args[0] + " : " + density + " : ");
if (density > 6.0)
System.out.println("heavy");
else
System.out.println("light");
} else
System.out.println("This is an error - denisty of zero.");
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
用法:Tut16_ReadText文件1 異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:0 在test1.main(test1.java:28)
test1.java的第28行是什麼? – iRuth 2015-02-24 01:00:09