Results 1 to 5 of 5
  1. #1
    Kerrai is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default ArrayList out of bound, split string error.

    Hey

    So i got this problem, i know whats wrong but i don't know how to fix it.

    So i am reading in a file and storing that to an array. I then want to split up the stored lines from the array.

    But when i want to "name" the tokens from the string.split, if there isnt any token at say result[7] i get the "java.lang.ArrayIndexOutOfBoundsException"

    So is there any way to tell the program that if result has a empty entry, either make one or at least tell that its 0 or no entry?

    I have been staring blind at this part for a while and then maybe the even obvious simple solution is hard to find.

    Thanks
    //Martin

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class BookFix {
    
    	public void Book() { 
    
              ArrayList<String> bookholder = new ArrayList<String>();
              String tmp = "";
              
              
              File fInput = new File("C:/Users/Kerrai/workspace/Assign Two/Books.txt");
              Scanner scInput = null;
              try {
     			scInput = new Scanner(fInput);
     		} catch (FileNotFoundException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		}
              while (scInput.hasNextLine()){
                 tmp = scInput.nextLine();
                 bookholder.add(tmp);
              }
              
              scInput.close();
          
     	     for (int i = 0;i < bookholder.size(); i++) {
     	     
     	    	 System.out.println("\n" + 
     	    			 "Book " + (i+1) + "\n" + bookholder.get(i) + "\n");
     	 
     		     String[] result = ((String) bookholder.get(i)).split("#+");
     		     for (int x=0; x<result.length; x++)
     		    	 System.out.println(result[x]);
     	     
     		     String isbn = result[0];
     		     String copyNumber = result[1];
     		     String title = result[2];
     		     String author = result[3];
     		     String publisher = result[4];
     		     String year = result[5];
     		     String statistics = result[6];
     		     String borrowDate = result[7];
     		     String returnDate = result[8];
     		     String libraryCardNumber = result[9];
     		    
     	     
          }
      }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    String borrowDate = (result.length > 7) ? result[7] : "";

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default eh?

    i couldn't reproduce your problem with the following code

    Java Code:
    public class Empty {
    	public static void main(String[] args) {
    		String[] result = new String[5];
    		String[] arr = "hallo#this##a#test".split("#");
    		for (int i = 0; i < arr.length; i++) {
    			System.out.println(arr[i]);
    			result[i] = arr[i];
    		}
    	}
    }

    the whole sentence should be "hallo this is a test". but even when "is" is missing the assignment to the result array cause no problem. :confused:

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That's not what he is doing however. That for loop applies only to the "println", for the rest he is doing var=array[hard-coded-index]

  5. #5
    Kerrai is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Java Code:
    String borrowDate = (result.length > 7) ? result[7] : "";
    Thank you so much.

    I also guess i could add this to each line if some early entry in the future is blank too.

Similar Threads

  1. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  2. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  3. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  4. [SOLVED] String out of bound error
    By maxb in forum New To Java
    Replies: 4
    Last Post: 12-01-2008, 02:32 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •