Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By syedejaz

Thread: LineCount code help

  1. #1
    syedejaz is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default LineCount code help

    Dear all
    I need some modifcations in the following code:

    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    public class CopyOfLineReaderExp {
    public static void main(String[] args) throws Exception {
    LineNumberReader lineCounter = new LineNumberReader(
    new InputStreamReader(new FileInputStream("d:\\MyFile.txt")));
    String nextLine = null;
    try {
    while ((nextLine = lineCounter.readLine()) != null) {
    if (nextLine == null)
    break;
    System.out.println(nextLine);
    }
    System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
    } catch (Exception done) {
    done.printStackTrace();
    }
    }
    }

    First I need error message if for instance MyFile.txt doest exist. It should display a message "File doesnt exist".
    Another thing is that I want to exclude } from the counting.
    Third thing is that if line starts with /* , it should ignore it until it comes to */.
    Please help me.

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: LineCount code help

    1. FileNotFoundException (Java Platform SE 7 )

    ... As far as the rest, do some work, if you have specific problems, ask for help.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: LineCount code help

    syedejaz, you ignored quad64bit's response in Help me in LinkedList function . Not the best way to continue to get free help on a forum, abandoning a thread without replying to responses and then asking another question.

    Also, go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    syedejaz is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: LineCount code help

    Quote Originally Posted by DarrylBurke View Post
    syedejaz, you ignored quad64bit's response in Help me in LinkedList function . Not the best way to continue to get free help on a forum, abandoning a thread without replying to responses and then asking another question.

    Also, go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    Sorry dear
    I am new to forum and I dont know the rules.
    Yes you are right but the answer was too late and I didnt feel to reply back.
    Anyhow I will try to take care next time.
    DarrylBurke likes this.

  5. #5
    syedejaz is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: LineCount code help

    import java.io.*;
    public class LineCountingProg
    {
    public static void main(String args[])
    {
    FileInputStream fis = null;
    try
    {
    fis = new FileInputStream("d://MyFile.txt");
    }
    catch(FileNotFoundException e)
    {
    System.out.println("The source file does not exist. " +e );
    }
    LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(fis));
    String nextLine = null;
    try {
    while ((nextLine = lineCounter.readLine()) != null) {
    if (nextLine == null)
    break;
    System.out.println(nextLine);
    }
    System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
    } catch (Exception done) {
    done.printStackTrace();
    }
    }
    }

    I am new to Java and trying to learn.
    I have done something in the above code but I dont know how to add condition if line starts with /* and ends with /* it should keep ignoring it ??

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: LineCount code help

    see String.charAt(int) String (Java Platform SE 7 )

  7. #7
    syedejaz is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: LineCount code help

    Quote Originally Posted by SJF View Post
    see String.charAt(int) String (Java Platform SE 7 )
    Thanks dear but as I told before I am new and dont know how to use it.
    Thanks once again

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: LineCount code help

    I've told you about using the code tags and provided a link to the page that explains how to use them. So, are you unable to understand that page or are you just plain lazy?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  4. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •