![]() |
for one more time here i am........ sorry guys....... :(
this time i am trying to create a java file that reads lines and adds each line to an array...here is the code...it compiles but i am getting an error message: exception in thread "main" java.lang.ArrayIndexOutOfBoundsException....:( :
import java.io.*; |
You are declaring a String array of zero length in the first pass of your first while loop. That is a String array with one element otherwise known as just a plain String (no array). In the second while loop, you are indexing your "array" past its length (which is ONE). Hence the java.lang.ArrayIndexOutOfBoundsException.
Try it with a Vector.... :
import java.io.*;Hope this helps |
Quote:
:
LineString[bob] = (String)v.elementAt( bob );since you already know that the vector contains strings (albeit ones that have had their cast widened to Object) - probably best to get into the habit of using casting in a situation like this, rather than calling methods on the elements as you retrieve them - because one day, in some situation or another - that won't work, and you'll be left wondering what to do... (eg if you are storing instances of an object you created yourself, will you start writing a 'toMyObject()' method? hehe you wouldn't want to go there ;) This is not a criticism of drunkenCoder's example - its just best if you know the other method as well - especially when you start storing other data types in vectors (or any other type of collection for that matter) and you don't have a method that does the casting for you... - like .toString(), or Double.parseDouble()... so yeah - remember - you don't need a method to recast an object as you remove it from a collection - just a cast. :) |
| All times are GMT -5. The time now is 12:43 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC