Fri Oct 24, 2008 9:12 pm
/*
***Start Java Out of Bounds Program***
*/
import java.io.*;
import java.util.Vector;
public class ListOfNums {
private vector number;
private static final int SIZE = 10;
public ListOfNums(){
numbers = new Vector(SIZE);
for(int i = 0; i < SIZE; i++);
number.addElement(new Integer(i));
}
}
public void writeList(){
PrintWriter out = null;
try {
System.out.println("Try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println(Value at: "+i+" "=" + numbers.elementAt(i));
}
} catch (ArrayIndexOutofBoundsException e) {
System.err.println("Caught ArrayIndexOutofBoundsException:" +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException:" + e.getMessage());
} finally {
if(out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
/* End Java Out of Bounds Program */
Fri Oct 24, 2008 9:14 pm
public class ListOfNums {
// ***myComment: vector should be capitalized Vector:
private vector number;
private static final int SIZE = 10;
public ListOfNums(){
// ***myComment: "numbers" should be "number":
numbers = new Vector(SIZE);
for(int i = 0; i < SIZE; i++); //<--***myComment: delete semicolon
number.addElement(new Integer(i));
}
}// <--***myComment: delete this closing brace
public void writeList(){
// ***myComment: may want to rename PrintWriter "out"
// *** it is easy to confuse with System.out...
// ***do not have to, but it is easier to debug:
PrintWriter out = null;
try {
System.out.println("Try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
// ***myComment: fix quotations and "numbers" should be "number":
out.println(Value at: "+i+" "=" + numbers.elementAt(i));
}
// ***myComment: fix spelling: ArrayIndexOut***Of***BoundsException:
} catch (ArrayIndexOutofBoundsException e) {
System.err.println("Caught ArrayIndexOutofBoundsException:" +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException:" + e.getMessage());
} finally {
if(out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
} // end try...catch
} // end writeList()
} // ***myComment: insert this brace to close the class definition
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.