Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Out of Bounds exception

Fri Oct 24, 2008 9:12 pm

I am trying to write a simple one-dimensional array and then
attempt to access any array element that is not within bounds. I.e.,
I have and int array of 10. What happens if I try to access 11, 20,
or even 100....hopefully you get the point. At any rate, I have the
following code, but it won't compile (especially at the public void
writeList(){ line. Any help on this code, or suggestions on how to
improve it would be appreciated. Who knows if I am even on the right
path...I hardly ever use Java, so it is hard for me to understand at
times.

Code:
/*
***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 */




Re: Out of Bounds exception

Fri Oct 24, 2008 9:14 pm

inserted some comments where I found some small syntax errors.
There are also a couple of misplaced braces. I find it helpful to
comment closing braces as I create them. It keeps me from pulling my
hair out later when things don't work ;)


Code:
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 0SIZEi++); //<--***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 0SIZEi++) {
// ***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 


Post a reply
  Related Posts  to : Out of Bounds exception
 Exception handling     -  
 Exception is not clear     -  
 Multiple Exception Catching     -  
 String Too Long Exception     -  
 Exception Handling in a project     -  
 Exception Handler in java     -  
 Define your own exception class     -  
 J2me Image load Exception     -  
 how to design a exception handling for a project     -  
 java.security Exception in Applet     -  

Topic Tags

Java Arrays, Java Exceptions