Results 1 to 9 of 9
  1. #1
    Kane87 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default How to remove comments and reserved words from a text file?

    First of all, hello everyone!

    I am not very good at programming, so I really need your help with this assignment that I have to do for college.

    I need a code that does the following:

    1) Removes all the comments from a text file.
    2) Removes reserved words from a text file (reserved words like int, object, float, date, bool etc. ).

    I will appreciate any kind of help. Thanks in advance!

  2. #2
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: How to remove comments and reserved words from a text file?

    Are you asking for help or making a request?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to remove comments and reserved words from a text file?

    Which types of comments? Java comments? Single line comments? Or multiline comments too?
    What's your approach?

  4. #4
    Kane87 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to remove comments and reserved words from a text file?

    I guess it's a request since I need someone to write me a whole code?

    Yes, it's Java comments, both single line and multline, so the ones after "//" and after "/*"

    I got this code from someone else, but it's not working properly. I can't reach that person at the moment, so I need your help! Here's what I got:


    private static String removeComments(String formulaText) {
    StringTokenizer st = new StringTokenizer(formulaText, "\n");
    StringBuffer stringBuffer = new StringBuffer();
    while (st.hasMoreElements()) {
    String nextToken = st.nextToken();

    if (nextToken.startsWith("//") || nextToken.startsWith("/*")) {

    // comments.add(nextToken);
    } else {
    stringBuffer.append(nextToken + "\n");
    }
    }
    return stringBuffer.toString();

    }

    I don't know why is that comment there in the if clause, since I don't even have a comments variable anywhere in the code...

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: How to remove comments and reserved words from a text file?

    Just to inform you.. this forum is made to help users through guidance. They don't do your homework for you.. they help you understand it so you can accomplish it yourself. If you want help, you should ask how these guys here can help you understand your code and how you can fix and improve it.

  6. #6
    Kane87 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to remove comments and reserved words from a text file?

    Can you help me with the code I just posted, then? I don't understand why it's not working and since I am really bad at programming, I need your help. Thanks!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: How to remove comments and reserved words from a text file?

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

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

  8. #8
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: How to remove comments and reserved words from a text file?

    I think that you should eliminate comments in two steps:

    1. Use the StringTokenizer like your code does, using the newline character, "\n" as a delimiter and then eliminate the // comments by scanning each String that you get, saving everything up until the // and then skipping everything after. This can be done with a for-loop, looking at each character in the line.

    2. For the multi-line comments, /* */, you can scan again the results of step 1 above looking for /* and when you find that, you skip everything until you find */

    Try your hand at writing some code, and if you have problems, post your code and we'll help you with it.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to remove comments and reserved words from a text file?

    I'd use the compiler itself for it: it is ideal for removing all sorts of comments and the AST tree API can reproduce fine source code for any syntactical unit of the compiled program; removing keywords is a breeze then ... its API is public since Java 1.5 (if I'm not mistaken).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Adding words to a text file using java
    By Andie in forum New To Java
    Replies: 0
    Last Post: 04-21-2012, 08:14 PM
  2. Read text file and count words.
    By chdn202002 in forum New To Java
    Replies: 5
    Last Post: 07-17-2011, 02:44 PM
  3. how to remove word words from txt file?
    By amroosobhy in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 01:49 PM
  4. Read in words from a text file that contains a story
    By Pleenen in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 04:34 AM
  5. Total number of reserved words in java
    By vivvy in forum New To Java
    Replies: 1
    Last Post: 02-06-2010, 03:18 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
  •