|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-09-2008, 01:22 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
|
[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  
__________________
Its all in your mind Buddy...cheers
|
|

05-09-2008, 04:07 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 237
|
|
|
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: 309
|
|
Given the contents of a.txt...
You should get some patterns there.
If the example you've given is like the quote below,
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.
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...
Formatter.format("%s\n", String);
__________________
best regards, 
sukatoa
Last edited by sukatoa : 05-09-2008 at 04:12 PM.
Reason: Additional.....
|
|

05-12-2008, 12:41 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
thnaks 
__________________
Its all in your mind Buddy...cheers
|
|

05-12-2008, 12:59 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
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
__________________
Its all in your mind Buddy...cheers
|
|

05-12-2008, 01:09 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
|
|
|
Did you debug and try to understand what happened there? It's not difficult to get an idea pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-12-2008, 01:15 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-12-2008, 01:21 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
|
yeah...but i am not getting the output in the desired manner..???
__________________
Its all in your mind Buddy...cheers
|
|

05-12-2008, 01:22 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
|
|
|
What you mean there? I'm not clear. You just need element by element to display?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-12-2008, 01:28 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
|
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..
__________________
Its all in your mind Buddy...cheers
|
|

05-12-2008, 01:33 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
|
|
|
Read a line, separate tokens and manipulate the required strings. I think it's the easiest way.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-12-2008, 02:07 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
|
thts ok..but if i want my O.P in the desired format...
__________________
Its all in your mind Buddy...cheers
|
|

05-13-2008, 03:42 AM
|
|
Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 78
|
|
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
Last edited by Eku : 05-13-2008 at 03:46 AM.
|
|

05-13-2008, 08:31 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 66
|
|
thanks for your advice...nice trick buddy...  ..
__________________
Its all in your mind Buddy...cheers
|
|

05-13-2008, 09:42 AM
|
|
Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 78
|
|
|
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
|
|
|
|
|