The number of characters are different. I think a simplier approach is to add a Delimeter. Then Cut the substring before the Delimeter. You can also check if there is a missing part in the line by counting the delimeters. Here is my suggestion.
S.No Description Qty. Units Rate (Rs.) Value (Rs.)
1$Aviation Lamp$1$Nos$3700$3,700$
2$Lighting Arrester$1$Nos$1600$1,600$
3$Aviataion Lamp Cable$31$Rmt$270$8,370$
Gross Total $13,670$
The above Total include Services Tax Amount of Rs $536$
Total Invoice Value $13,670$
thats how should it look. You can change the '$' sign if you like.
Then, extract all the content of the file into a single String.
Then, get all the Lines and save it into a variable.
Here is how i extracted the Number of Lines and lines in a Single String.
String LineFeed=null,line=null,Output=null; int ctr=0;
BufferedReader in = new BufferedReader(new FileReader("A.txt"));
while ((line = in.readLine()) != null) {
LineFeed += line;
ctr++;
} LineFeed = LineFeed.substring(4);
Here is where i segregate the desired output i want
int ptr = ctr-4; String LineFeed2 = LineFeed;
for (i=1; i<ptr;i++){
try{
Output += "S.No " + LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Description "
+ LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Qty. " + LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Units " + LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Rate (Rs.) "
+ LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Value (Rs.) "
+ LineFeed2.substring(0,LineFeed2.IndexOf("$")+"\n"+"\n";
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
} catch (Exception A){
System.out.println("Error in Line Number: " + ptr+1);}
} LineFeed2 = LineFeed2.substring(4); //removing the null =)
//Adding the Last Line
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Gross Total " + LineFeed2.substring(0,LineFeed2.IndexOf("$");
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Tax Amount of Rs "
+ LineFeed2.substring(0,LineFeed2.IndexOf("$");
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
Output += "Total Invoice Value "
+ LineFeed2.substring(0,LineFeed2.IndexOf("$");
LineFeed2 = LineFeed2.substring(IndexOf("$")+1);
//Here is the output you Desire
System.out.println(Output);
I Hope that helps. God BLess in your Code