Results 1 to 8 of 8
  1. #1
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Fixed Length records parsing

    Hello all,

    I have been trying for the last few days to parse a fixed length file that contains records

    that would look something like this:

    name address city state zip
    john 123 main st st louis mo 63021
    samantha 210 elm st union mo 63325
    frank 455 pine ave washington mo 63090
    (spaces between fields should be fixed lengths)

    but i cannot for the life of me get anything to accept the whitespace

    i've tried code like this:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class msd_split { //class header
    	public static void main(String[] args) throws IOException { // main()
    		String file = args[0]; 
    		String lineFromFile = ""; 
    		FileInputStream fileStream = new FileInputStream(file);
    		DataInputStream input = new DataInputStream(fileStream);   
    		BufferedReader readFile = new BufferedReader(new InputStreamReader(input));
    
    		while ((lineFromFile = readFile.readLine()) != null) {
                                 String hold1 = lineFromFile.substring(34,35);
    	                  String hold2 = lineFromFile.substring(35,36);
                                 System.out.println(hold1 + "," + hold2)
                           }
                           readFile.close();
              }
    }
    Last edited by steveo314; 10-17-2012 at 07:28 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Fixed Length records parsing

    Please use [code] tags [/code] when posting code.

    What problem are you seeing?
    In other words, what should be happening compared to what is happening?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Fixed Length records parsing

    I'm trying to check parts of the string at the substring locations. It gives me the StringOutOfBoundsException if i remember right.

    Edit -
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 35
    at java.lang.String.substring(Unknown Source)
    at msd_split.main(msd_split.java:25)
    Last edited by steveo314; 10-17-2012 at 07:33 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Fixed Length records parsing

    So, that means the string is not at least 36 characters long.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Fixed Length records parsing

    The string is 1129 character spaces (characters and whitespace) but I'm guessing with
    the substring() it won't read past multiple \s (like: \s\s\s\s\s\s)????

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Fixed Length records parsing

    substring doesn't care what the character is.

    Print out the line you think is 1129 characters long that is throwing this exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Fixed Length records parsing

    And why all those streams?
    You just need:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader("my file"));
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Replies: 12
    Last Post: 10-25-2012, 05:26 PM
  2. Replies: 1
    Last Post: 09-06-2012, 11:53 AM
  3. Difference between length() and length
    By mitra in forum New To Java
    Replies: 7
    Last Post: 07-28-2011, 12:46 AM
  4. Fixed location
    By Dipke in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:25 AM
  5. Fixed Header
    By Quirl in forum New To Java
    Replies: 2
    Last Post: 07-28-2010, 10:19 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
  •