Results 1 to 16 of 16
  1. #1
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Extarctin substring

    Hi everyone

    I am new in java, I am working on a reseach project that I need extyract some sustring from some huge text file, but my problem is that in each line there are many missing data that is blank(space) and the program that I have writen, give me the : java.lang.StringIndexOutOfBoundsException error when it reach to the first misssing part and stop,

    please help me if you know how can I handle this problem

    my code is here:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class DataReader {
    
    	public static void main(String[] args){
    
    		Scanner stdIn = new Scanner(System.in);
    		Scanner fileIn;
    		String record;
    		
    		try{
    			System.out.println("Enter input file name");
    			fileIn = new Scanner(new FileRead(stdIn.nextLine()));
    			
    			while (fileIn.hasNext()){
    				record = fileIn.nextLine();
    								
    				System.out.println(
    						record.substring(1,20)+ ", " + 
    						record.substring(138, 168)+ ", " +
    						record.substring(168, 198)+ ", " +
    						record.substring(198, 228)+ ", " +
    						record.substring(228, 258)+ ", " +
    						record.substring(258, 288)+ ", " +
    						record.substring(288, 318)+ ", " +
    						record.substring(318, 348)+ ", " +
    						record.substring(348, 378)+ ", " +
    						record.substring(378, 408)+ ", " +
    						record.substring(408, 438)+ ", " +
    						record.substring(438, 468)+ ", " +
    						record.substring(468, 498)+ ", " +
    						record.substring(498, 528)+ ", " +
    						record.substring(528, 558)+ ", " +
    						record.substring(558, 588)+ ", " +
    						record.substring(588, 618)+ ", " +
    						record.substring(618, 648)+ ", " +
    						record.substring(648, 678)+ ", " +
    						record.substring(678, 708)+ ", " +
    						record.substring(708, 738)+ ", " 
    						); 
    							}
    			fileIn.close();
    		}
    		catch (FileNotFoundException e)// for fileIn
    		{
    			System.out.println("Error:" + e.getMessage());
    		} 
    
    	}
    
    }

    Sincerely
    Cowboy2010
    Last edited by Eranga; 07-29-2010 at 07:00 AM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    give me the : java.lang.StringIndexOutOfBoundsException error
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeah post the complete error message here to see.

    Java Code:
    record.substring(708, 738)
    Looking at the above line of code, are you sure that those indexes are exist in your string, means is that much lengthy line of strings are?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP, also please use code tags when you are posting here again. Unformated codes are really hard to read.

  5. #5
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default the error message

    Hi
    the error message is this :

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 20
    at java.lang.String.substring(Unknown Source)
    at DataReader.main(DataReader.java:23)


    thanks for your help
    Cowboy

  6. #6
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    this is error message

    :
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 20
    at java.lang.String.substring(Unknown Source)
    at DataReader.main(DataReader.java:23)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What is the length of the String being substringed at line 23?
    Is its length less than 20 characters and you're trying to get the 20th char?
    If so, you will get the error: "String index out of range: 20"

    Before you do: substring(708, 738)
    You should test that the String is at least 738 characters long.

  8. #8
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    the problem is that in some rows the string is long enough to do that but in many of the data rows there are missing part that caous that string be shorter than what it suppose to be, and actual data file is huge, and unfortunately for those missing parts there is no space saved as charecter, if it was space instead of empty line it wasn't problem but it is nothing, so what should I do ? do you have any Idea ?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To be able to parse(extract columns/fields) records/lines of different lengths you need a way to identify what the fields are contained in each record. If some fields/columns are missing and there is no way to detect that they are missing, then there is no way to process the columns in the record.
    If each field/column can be identified as to which column it is, then you can detect that one is missing if they are always in the same order: first field, second field, ... nth field.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Also, as a side note, you should place the fileIn.close() statement in a finally block after the catch block. That way, that statement is guaranteed to be executed, regardless of what happens in the try block.
    Learn something new... wiser-today.blogspot.com

  12. #12
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Actually, I was thinking about it, and it seems to me (anyone stop me if I'm completely wrong!) that you might be better off using the Scanner object to parse each line as you are doing, but then use another Scanner object to parse each line into an array of Strings. Then you could query the array's size to determine if all columns were present. Of course, I'm making the assumption that each line contains some sort of delimiter that separates each column.
    Learn something new... wiser-today.blogspot.com

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually no need to use two scanner objects to do this, you can use the same object to validate before pass in to the sub-string process.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cowboy2010 View Post
    the problem is that in some rows the string is long enough to do that but in many of the data rows there are missing part that caous that string be shorter than what it suppose to be, and actual data file is huge, and unfortunately for those missing parts there is no space saved as charecter, if it was space instead of empty line it wasn't problem but it is nothing, so what should I do ? do you have any Idea ?
    I'd say build your own little method that anticipates on a too short String; maybe the following will do:

    Java Code:
    public String mySubString(String s, int start, int len) {
    
       if (s == null) return ""; // no string
       int n= s.length();
       if (start >= n || end >= n) return ""; // String too short
       return s.substring(start, end);
    }
    kind regards,

    Jos

  15. #15
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Actually no need to use two scanner objects to do this, you can use the same object to validate before pass in to the sub-string process.
    Good point!
    Learn something new... wiser-today.blogspot.com

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  2. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  3. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  4. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 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
  •