I have created a method to write in a file, as shown in the code below. This method is called 1000 times from another method, with the two given parameters.
However in 5 of these calls, this method catches an error “e” and prints out: java.lang.ArrayIndexOutOfBoundsException and in my terminal I get the result of System.out.println(“”+(double)C/T), but in the file this result is missing. (the other calls work fine)
I was really confused in the beginning, as I am not working with arrays at all. After googling a little bit, I found this: http://www-01.ibm.com/support/docview.wss?uid=swg1IZ87600
I am still really confused and have no idea how to fix this. Somebody help please?
public void myMethod(int C, int T) {
try {
FileOutputStream fout = new FileOutputStream ("output.txt", true );
PrintStream ps = new PrintStream(fout);
System.out.println(""+(double)C/T);
ps.println((double)C/T+"");
// close file
fout.close();
}
catch(Exception e) {
System.err.println(e);
}
}
Worked perfectly for me on my Windows JVM:
I’d write it this way: conforms to the Java coding standards and uses names that are clearer: