Total members 11678 |It is currently Sat Nov 25, 2017 8:49 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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





Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

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 


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 Catch database connection exception     -  
 J2me Image load Exception     -  
 how to design a exception handling for a project     -  



Topic Tags

Java Arrays, Java Exceptions







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team