Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 10th, 2004, 10:43 PM   #1
FidI
Newbie
 
Join Date: Nov 2004
Posts: 7
Rep Power: 0 FidI is on a distinguished road
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());
  	
             

            }
        }



    }
}
FidI is offline   Reply With Quote
Old Nov 18th, 2004, 6:01 AM   #2
drunkenCoder
Newbie
 
drunkenCoder's Avatar
 
Join Date: Nov 2004
Posts: 8
Rep Power: 0 drunkenCoder is on a distinguished road
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
drunkenCoder is offline   Reply With Quote
Old Nov 21st, 2004, 7:00 PM   #3
eccles
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 eccles is on a distinguished road
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.
eccles is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC