Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2008, 01:22 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
[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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-09-2008, 04:07 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 237
sanjeevtarar is on a distinguished road
Is your input file format is fix like you have given....

and you want output as a .txt file.....


__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-09-2008, 04:07 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 309
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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);
__________________
best regards,
sukatoa

Last edited by sukatoa : 05-09-2008 at 04:12 PM. Reason: Additional.....
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-12-2008, 12:41 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
thnaks
__________________
Its all in your mind Buddy...cheers
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-12-2008, 12:59 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
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
__________________
Its all in your mind Buddy...cheers
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-12-2008, 01:09 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-12-2008, 01:15 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-12-2008, 01:21 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
yeah...but i am not getting the output in the desired manner..???
__________________
Its all in your mind Buddy...cheers
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-12-2008, 01:22 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-12-2008, 01:28 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-12-2008, 01:33 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-12-2008, 02:07 PM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
thts ok..but if i want my O.P in the desired format...
__________________
Its all in your mind Buddy...cheers
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-13-2008, 03:42 AM
Eku Eku is offline
Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 78
Eku is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-13-2008, 08:31 AM
jazz2k8's Avatar
Member
 
Join Date: Apr 2008
Posts: 66
jazz2k8 is on a distinguished road
thanks for your advice...nice trick buddy.....
__________________
Its all in your mind Buddy...cheers
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 05-13-2008, 09:42 AM
Eku Eku is offline
Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 78
Eku is on a distinguished road
No Problem . . . =)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Step-by-Step Tutorial: Achieve RAD with Seam+Eclipse+Tomcat Techieexchange JavaServer Faces 0 11-13-2007 08:13 PM


All times are GMT +3. The time now is 10:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org