Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Problem With "valueof" (http://www.programmingforums.org/showthread.php?t=1100)

FidI Nov 10th, 2004 11:43 PM

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.*;
import java.util.*;

public class token4
{
    public static void main(String args[]) throws IOException{
        String question;
        StringTokenizer currentString;
  //open file
        BufferedReader InputData = new BufferedReader(new FileReader("./inputData.txt"));

  int i = 0;
  //read each line
        while ((question = InputData.readLine()) != null){
          //new string array
    String[] LineString = new String[i];
  i++;
            currentString = new StringTokenizer(question,".");
             

          while (currentString.hasMoreElements()) {
          //add Strings to array...
          LineString[i] = String.valueOf(currentString.nextElement());
         
           

            }
        }



    }
}


drunkenCoder Nov 18th, 2004 7:01 AM

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.*;
import java.util.*;

public class token4
{
  public static void main(String args[]) throws IOException
  {
      String question;
      StringTokenizer currentString;
      //use a Vector since you don't know how many sentences the file
      //will be
      Vector v = new Vector();
      String temp;
      int i = 0;
      //i changed the path of the file
      FileReader file = new FileReader("C:/inputData.txt");
      BufferedReader InputData = new BufferedReader( file );
      //read each line of the file
      while ((question = InputData.readLine()) != null)
      {
          currentString = new StringTokenizer(question,".");
          while (currentString.hasMoreElements())
          {
            //store sentences in temporary string
            temp = String.valueOf(currentString.nextElement());
            //add the string to the Vector
            v.addElement( temp );
            //print statement for debugging
            //System.out.println(v.elementAt( i ));
            //i++;
          }
      }
      //if you need the data in string arrays, the portion of code below
      //does this for you
      //declare String array for length of vector
      String[] LineString = new String[v.size()];
      //iterate through the vector, putting each element into an index
      //of "LineString"
      for (int bob = 0; bob < v.size(); bob++ )
      {
        LineString[bob] = v.elementAt( bob ).toString();
        //print statement for debugging
        //System.out.println( LineString[bob] );
      }
 
  }
}


Hope this helps

eccles Nov 21st, 2004 8:00 PM

Quote:

LineString[bob] = v.elementAt( bob ).toString();
alternatively, just recast it to a string:

:

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