• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

print only the single and multiple line comments when reading a java sample code

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with this program i want to print only the comments messages but it does not work it is also possible that i check with indexof but i dont know how to use any body help please

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the issue with the current code?
What is it not doing?

One thing that does come to mind.  What if there is white space before the '//' or '/*'?
Or  anything, for that matter?
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please wrap your code using code tags next time.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Block comments end with "*/", your program checks for *\ instead.
 
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really need to clean up the indentation. Your current indentation is hiding some fundamental flaws. I would also insist that you include braces after  each and every if(), else, while, for, etc.. Without them the code is not doing what you expect. Post your cleaned up version back here.

What happens when you have two if's back to back as you do on lines 7 and 10?
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please check this one it will read the attached file, the indentation is print only the comments when reading Sum.txt


BUILD SUCCESSFUL (total time: 1 second)
but I need like print only singl line and multiple line comments like bellow

this is the first program

sum of the number

the total number going to sum

count ++

it is the result
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you have forward slashes (correct) in one place and backwards slashes in the other.
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not handling the case where the comment is all on one line.


Do you have a sample file that you are running through your program, and does that file have all the possible comment variations that you can come up with?
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WHILE line is not equal to null OR line does NOT contain "*/"
Notice that it can't get to the OR expression unless line IS null, in which case you'll get a null pointer exception.
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your indentation is better but could be even better than that.
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Here you have forward slashes (correct) in one place and backwards slashes in the other.


Also, your substring will be all characters up to "\\" instead of after. Note that "\\" should have been "//".
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Second if should have been an else-if. What do you expect to happen for this comment:

What about this case:
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the reply, yes you are right is forward slash "//" and actually I did not expect those condition now it will be better to consider those conditions as well, and i attached the sample file that I am using please have it and help me to solve the issue because tomorrow i have to submit my homework
 
Marshal
Posts: 28261
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are more conditions which you haven't considered. Like for example a line of code which you posted here:



That /* doesn't start a comment because it's inside a string literal.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Sheriff, as my program knowledge is not that much good and I could not applied those conditions, could you please help for few that how do I apply, thank you
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammad Ali Raisi wrote:thank you for the reply, yes you are right is forward slash "//" and actually I did not expect those condition now it will be better to consider those conditions as well, and i attached the sample file that I am using please have it and help me to solve the issue because tomorrow i have to submit my homework


I don't see the example file attachment anywhere. Could you re-post it?

Extracting all Java comments in every use case is a non-trivial undertaking. I would assume for the moment that all that is needed to meet the requirements is to be able to parse the example file.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you the bellow code is he sample file could, I already attached but no problem, please save it in a txt file and do the operation.

package javaapplication5;

/**
*
* @author MOHAMMADALI
*/
public class Sum {
   
   
   
   
 public static void main(String[] args){
   

     
     int sum=0;
      /*
        this is the first program
      */
     //sum of the number
     int n = 10;// the total number going to sum
     int count = 0;
     //loop to sum all number
     for(int i=0; i<n;i++)
     {
         //count ++;
         sum= sum+ i;
     }
     System.out.println("the sum of the numbers are ="+ sum);
     //it is the result
     // this is /* a test1
     /* this is a test // program */
     
 }
   
}

 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one way is doing by indexof that search character by character and then print the needed part but i could not do.
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use regex for this . The following regex escapes * to match it literally and uses group () to print only contennt between block comments.

Prints:

 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, I think this is not a good solution because it makes program static, that we could not put all comments one by one may be we have many comments it dynamically must understand those and print please help for the previous code that i wrote
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've undoubtedly made some changes since the last post of your code. Could you re-post the current version of your code so that we may comment on that?
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a very nice program but it remove the comments and print other content of txt file so please tell me where i have to change that it prints comments and remove the other contents.



Output

public class Sum {
   
   
   
   
 public static void main(String[] args){
   

     
     int sum=0;
     
     
     System.out.println("please enter the number");
     int n = 10;
     int count = 0;
     
     for(int i=0; i<n;i++)
     {
         
         sum= sum+ i;
     }
     System.out.println("the sum of the numbers are ="+ sum);
     
     
     
     
 }
   
}BUILD SUCCESSFUL (total time: 0 seconds)

but i want the output like bellow


this is the first program

count ++

it is the result



 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the content of file which i read in the above program

public class Sum {
   
   
   
   
 public static void main(String[] args){
   

     
     int sum=0;
      /*
        this is the first program
      */
     //sum of the number
     System.out.println("please enter the number");
     int n = 10;// the total number going to sum
     int count = 0;
     //loop to sum all number
     for(int i=0; i<n;i++)
     {
         //count ++;
         sum= sum+ i;
     }
     System.out.println("the sum of the numbers are ="+ sum);
     //it is the result
     
     
 }
   
}

 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to turn a program that does one thing (i.e. remove comments) is not a good way to come up with a program to serve the opposite purpose (i.e. extract comments).

The problem is tricky. You need to detect three states: string constant, slash-slash comment, and slash-star comment. You could read the file line-by-line but as you found out it takes a little thought to handle slash-start comments that may bridge multiple lines or may have multiple occurrences on a single line. So, you could instead go character-by-character but this would involve setting one or more flags to determine what state you're in.

Another approach would be to load the entire file into a StringBuilder and then have a 'pos' variable that you increment through the string using charAt(). What this gives you is an easy way to look ahead  one character. As an example:
It also greatly simplifies things by allowing you to create three separate parse methods that are independent of each other.

 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the help I could not get your code, could you please put the complete code as i told i don't have programming background but I try my best to learn
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is my friend
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't like to do peoples' homework here. We will give helpful hints and answer specific questions, and occasionally provide snipits of code. It sounds like your instructor may not have given you enough background to complete this assignment. If you have any specific questions, ask away.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is specific question that i wrote a program asked you for help but you suggest me another one that i did not understand your code i asked for complete one it is not suppose to submit that one because my homework is parser not this but it is part of that i did with above code and it had error i asked you to please resolve the error
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you posted yesterday a 9:49am can be made to handle some of the comments if you limit the scope to:

And forget about:


I've already mentioned some correction that you need to make.
Line 16: needs to be else-if
Line 15: Needs substring of line AFTER "//".
Line 15: Change "\\"
Line 18: You boolean expression needs to be fixed
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, do you think in real code we have those kinds of comments that you mentioned?
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are rarer but they do occur. Unfortunately handling those cases means that your code would need a redesign so as to read character by character instead of by line. This gets quite a bit messier and I would point you back to my post today at 10:46am
 
Paul Clapham
Marshal
Posts: 28261
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Extracting all Java comments in every use case is a non-trivial undertaking.



We haven't talked about Unicode escapes, either. It's extremely unlikely that you would find a Java program where the // starting a comment had been Unicode-escaped, but nevertheless it's part of the Java specification and it's the sort of thing that some instructors would be sure to use as a test case.
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're interested, this is my test file from when I developed a similar utility.

 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread has been quite for 4 days so I'm going to assume that the issue is closed and that it would be safe to post the answer from some code I have.

 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
thank you for such a great help it works very well
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Mr. Carey Brown I have learned many things from your code
 
Carey Brown
Saloon Keeper
Posts: 10783
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammad Ali Raisi wrote:thank you Mr. Carey Brown I have learned many things from your code


You're welcome.

I hope I didn't post the answer too soon.

You can see how there's many subtleties to this problem.
reply
    Bookmark Topic Watch Topic
  • New Topic