Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Display output from a file using regular expression

  1. #1
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Display output from a file using regular expression

    Purchase Items
    Order 458
    Invoice No

    Date Bill noBill DateDescriptionPurpose Qty Rate/unitAmt Remark
    12/05/05 152 11/06/08 abc xyz 12 15 250 Abcef
    11/05/05 2561 12/07/08 wtc prvn 25 2 2530 good
    12/05/05 8695 05/05/08 pen Abcd 12 21 24 Bad
    12/05/08 8956 06/05/08 Pencil Drawing 5 2 10 good

    Team member: 5 Authorised By: praveen
    Employee ID & Signature: 1478 praveen Signature
    Team Leader: abcd Accountant:ramu
    Received Date:12/05/08
    I am in need of
    12/05/05 152 11/06/08 abc xyz 12 15 250 Abcef
    11/05/05 2561 12/07/08 wtc prvn 25 2 2530 good
    12/05/05 8695 05/05/08 pen Abcd 12 21 24 Bad
    12/05/08 8956 06/05/08 Pencil Drawing 5 2 10 good
    But as per My code out put is this format
    12/05/05 152 11/06/08 abc xyz 12 15 250 Abcef
    11/05/05 2561 12/07/08 wtc prvn 25 2 2530 good
    12/05/05 8695 05/05/08 pen Abcd 12 21 24 Bad
    12/05/08 8956 06/05/08 Pencil Drawing 5 2 10 good
    Team member: 5 Authorised By: praveen
    Employee ID & Signature: 1478 praveen Signature
    Team Leader: abcd Accountant: ramu
    Received Date:12/05/08
    My code
    class purchasevchr {

    public static String inputFile = "purchase.txt";//Input file
    purchase pfr = new purchase();

    public void formatFile() {
    {

    try {
    FileInputStream in = new FileInputStream(inputFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    String regEx1 = "(\\d+/)";//date format is matched here
    Pattern regEx1P = Pattern.compile(regEx1);


    while ((strLine = br.readLine()) != null) {

    System.out.println(strLine);//line by line printing
    }

    } catch (Exception e) {
    System.out.println("Exception " + e);
    }
    }

    }

    public class sample{

    public static void main(String args[]){
    purchasevchr ob=new purchasevchr();
    ob.formatFile();
    }
    }
    i tried with the code given by you..
    /*


    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class expense1 {

    public static String inputFile = "expense.txt";

    public void formatFile() {

    try
    {
    FileInputStream in = new FileInputStream(inputFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    String regEx1 = "([0-9]/)";
    Pattern regEx1P = Pattern.compile(regEx1);

    System.out.println("Date " + "Bill " + "noBill" + "DateDescription" + " Purpose" +
    "Qty" + "Dist " + "Rate/unitAmt" + "Remark");

    while((strLine = br.readLine())!= null)
    {
    Matcher match1 = regEx1P.matcher(strLine);

    if (match1.find())
    {
    System.out.println(strLine);
    }

    //System.out.println(strLine);
    }
    }
    catch (Exception e)
    {
    System.out.println("Ouch! " + e);
    }
    }

    public static void main(String[] args) {
    expense1 exp = new expense1();
    exp.formatFile();

    }
    }
    output i am getting is:
    Date Bill noBillDateDescription PurposeQtyDist Rate/unitAmtRemark
    12/05/05 152 11/06/08 abc xyz 12 15 250 Abcef
    11/05/05 2561 12/07/08 wtc prvn 25 2 2530 good
    12/05/05 8695 05/05/08 pen Abcd 12 21 24 Bad
    12/05/08 8956 06/05/08 Pencil Drawing 5 2 10 good
    Received Date:12/05/08
    at the end i am also getting the "Received Date:12/05/08".i dnt want this

    can you help me in this..

    thanks in advance.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Red face Re: Output Problem

    Try adding this to my code. Find:

    if (match1.find())
    {
         System.out.println(strLine);
    }

    And add this extra If statement so the new block looks like this:

    if (match1.find())
    {
         if (strLine.contains("Received") || strLine.contains("received") )
         {
              break;
         }
     
            System.out.println(strLine);
    }

    This will stop the Received Date:12/05/08 being picked up at the end.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Fixing of bug for simple GreenFoot game.
    By Nkay11 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 1st, 2009, 02:17 PM
  2. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM