Results 1 to 15 of 15
  1. #1
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default [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:confused::o

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Is your input file format is fix like you have given....

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


    sanjeev,संजीव

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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.

    Java 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...

    Java Code:
    Formatter.format("%s\n", String);
    Last edited by sukatoa; 05-09-2008 at 03:12 PM. Reason: Additional.....
    freedom exists in the world of ideas

  4. #4
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Lightbulb

    thnaks :)

  5. #5
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Did you debug and try to understand what happened there? It's not difficult to get an idea pal.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  8. #8
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    yeah...but i am not getting the output in the desired manner..???

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    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..

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Read a line, separate tokens and manipulate the required strings. I think it's the easiest way.

  12. #12
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    thts ok..but if i want my O.P in the desired format...

  13. #13
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    6

    Default

    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.
    Java 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
    Java 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 02:46 AM.

  14. #14
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    thanks for your advice...nice trick buddy...:)..

  15. #15
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    6

Similar Threads

  1. Step-by-Step Tutorial: Achieve RAD with Seam+Eclipse+Tomcat
    By Techieexchange in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 11-13-2007, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •