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:
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();
}
}
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?
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)
Re: Fixed Length records parsing
So, that means the string is not at least 36 characters long.
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)????
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.
Re: Fixed Length records parsing
And why all those streams?
You just need:
Code:
BufferedReader br = new BufferedReader(new FileReader("my file"));
Re: Fixed Length records parsing