
05-09-2008, 01:22 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
[SOLVED] How to achieve this?
i am having data like this:Input say it as a.txt
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
i would like to get this data in the form of:Output should be
S.No 1
Description Aviation Lamp
Qty. 1
Units Nos
Rate (Rs.) 3700
Value (Rs.) 3700
S.No 2
Description Lighting Arrester
Qty.1
Units Nos
Rate (Rs.) 1600
Value (Rs.) 1600
S.No 3
Description Aviataion Lamp Cable
Qty.31
Units Nos Rmt
Rate (Rs.) 270
Value (Rs.) 8,370
Gross Total 13,670
Tax Amount of Rs 536
Total Invoice Value 13,670
--------------------------------
I tried with Buffered Reader, String Buffer used some regex code.but i am unable to get this..can any body help me in this..one more thing i want s to generate this should work dynamically...If we give any input .txt file the corresponding text should be convert into that format 
|
|

05-09-2008, 04:07 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 254
Rep Power: 2
|
|
|
Is your input file format is fix like you have given....
and you want output as a .txt file.....
__________________
sanjeev,संजीव
|
|

05-09-2008, 04:07 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
|
|
Given the contents of a.txt...
You should get some patterns there.
If the example you've given is like the quote below,
|
Quote:
|
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
|
You can use Scanner class for this.
|
Code:
|
while(scannerObject.hasNext()){
s.no = scannerObject.next();
description = scannerObject.next();
qty = scannerObject.next();
unit = scannerObject.next();
rate = scannerObject.next();
value = scannerObject.next();
print those values....
} |
Try to have some experiments from it....
(Printing on console may be a good tests)
After that, read the Formatter class....
You can format it like the format you've post...
|
Code:
|
Formatter.format("%s\n", String); |
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
Last edited by sukatoa; 05-09-2008 at 04:12 PM.
Reason: Additional.....
|
|

05-12-2008, 12:41 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
thnaks
|
|

05-12-2008, 12:59 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
|
Quote:
|
public class TabData2 {
static String Sno,Qty,Description,Unit,Rate,Value;
public static void main(String args[]) throws FileNotFoundException{
File file = new File("D:\\bharath\\CSV\\tab2.txt");
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
// String line = scanner.nextLine();
// while(scanner.hasNext()){
Sno = scanner.next();
Description = scanner.next();
Qty = scanner.next();
Unit = scanner.next();
Rate = scanner.next();
Value = scanner.next();
Formatter fmt=new Formatter();
fmt.format("%s\n",Sno);
fmt.format("%s\n",Description);
fmt.format("%s\n",Qty);
fmt.format("%s\n",Rate);
fmt.format("%s\n",Value);
System.out.println(Sno);
System.out.println(Description);
System.out.println(Qty);
System.out.println(Rate);
System.out.println(Value);
}
}
}
|
i AM GETING ERRORS:My Output is:
sno
Description
Qty.
Rate
(Rs.)
Value
(Rs.)
1
Lamp
1
Nos
3700
3,700
Lighting
Arrester
1
Nos
1600
3
Aviataion
Lamp
Cable
31
270
8,370
Gross
Total
13,670
above
Total
include
Services
Tax
of
Rs
536
Total
Invoice
I
13,670
Now
Claimed
100%
Invoice
Value
13,670
(Rupees
Thirteen
Six
Hundred
and
Seventy
only)
Description
Qty.
Rate
(Rs.)
Value
1
Lamp
1
Nos
3700
Lighting
Arrester
1
Nos
1600
Aviataion
Lamp
Cable
31
270
Gross
Total
13,670
above
Total
Services
Tax
of
Rs
536
Invoice
I
13,670
Now
Claimed
Invoice
Value
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at TabData2.main(TabData2.java:31)
any one correct this..please..thnx...My desired output is :
S.No 1
Description Aviation Lamp
Qty. 1
Units Nos
Rate (Rs.) 3700
Value (Rs.) 3700
S.No 2
Description Lighting Arrester
Qty.1
Units Nos
Rate (Rs.) 1600
Value (Rs.) 1600
S.No 3
Description Aviataion Lamp Cable
Qty.31
Units Nos Rmt
Rate (Rs.) 270
Value (Rs.) 8,370
Gross Total 13,670
Tax Amount of Rs 536
Total Invoice Value 13,670
|
|

05-12-2008, 01:09 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
Did you debug and try to understand what happened there? It's not difficult to get an idea pal.
|
|

05-12-2008, 01:15 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
In simple word, in you file has 47 elements. Each time you read you read bundle of 6 elements. So what happened is, in last read you have only 5 elements. No 6th element.
Simply you get an exception.
|
|

05-12-2008, 01:21 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
|
yeah...but i am not getting the output in the desired manner..???
|
|

05-12-2008, 01:22 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
What you mean there? I'm not clear. You just need element by element to display?
|
|

05-12-2008, 01:28 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
|
As i told you in my posts my output should be in :
S.No 1
Description Aviation Lamp
Qty. 1
Units Nos
Rate (Rs.) 3700
Value (Rs.) 3700 format..
|
|

05-12-2008, 01:33 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
Read a line, separate tokens and manipulate the required strings. I think it's the easiest way.
|
|

05-12-2008, 02:07 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
|
thts ok..but if i want my O.P in the desired format...
|
|

05-13-2008, 03:42 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 234
Rep Power: 2
|
|
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.
|
Code:
|
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
|
Code:
|
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
Last edited by Eku; 05-13-2008 at 03:46 AM.
|
|

05-13-2008, 08:31 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 144
Rep Power: 0
|
|
thanks for your advice...nice trick buddy...  ..
|
|

05-13-2008, 09:42 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 234
Rep Power: 2
|
|
|
No Problem . . . =)
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 07:48 PM.
|
|