Exercise 6.11: stripComments

Posted on 2014-01-10
1 Endorsement
Last Modified: 2014-01-16
I am trying to resolve a problem on the website called Practice-It:

Write a method called stripComments that accepts a Scanner representing an input file containing a Java program as its parameter, reads that file, and then prints the file's text with all comments removed. A comment is any text on a line from // to the end of the line, and any text between /* and */ characters. For example, consider the following text:

import java.util.*;

/* My program
by Suzy Student */
public class Program {
    public static void main(String[] args) {
        System.out.println("Hello, world!"); // a println

    public static /* Hello there */ void foo() {
        System.out.println("Goodbye!"); // comment here
    } /* */
If the file contained this text, your program should output the following text:

import java.util.*;

public class Program {
    public static void main(String[] args) {
        System.out.println("Hello, world!");

    public static  void foo() {

I am not a student but am trying to learn coding on my own.
I would appreciate some guidance.

public static void stripComments(Scanner input){

    String line = "";
        line = input.nextLine();
        if(line.contains("/*") && !line.contains("*/" )){
        }else if( line.contains("*/")){
        }else if(line.contains("//") ){
            int index = line.indexOf("//");
            System.out.println(line.substring(index-index, index));

Open in new window

Question by:DOCDGA
  • 4
  • 2
LVL 32

Assisted Solution

phoffric earned 250 total points
ID: 39772914
how about 0 instead of index-index.
Your line approach is acceptable, although a char-by-char approach may be easier to write.
Using line by line, when you encounter a /*, then you can immediately print up to the index, and set a foundComment flag. If */ not on that line, then you are done.

When reading in new lines, if the foundComment flag is true, keep searching for the */ on that line and don't print the line if not found.

I think given the code you showed, you may be able to take it from there for other cases.
LVL 32

Accepted Solution

awking00 earned 250 total points
ID: 39773716
The file I attached writes the text without comments to the console and also creates a file without the comments from the text you supplied as an example, although I modified it somewhat to accommodate text like -
/* comment starts here
comment goes on here or
* goes on here and
*/ comment ends here
Let me know if ou need any further explanation.

Author Closing Comment

ID: 39776730
Thanks awking00 and phoffric

awking00, I don't see the attachment
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

LVL 32

Expert Comment

ID: 39777488
I only supplied that because the asker stated he was not a student in his initial question but was trying to learn using a practice website.

Author Comment

ID: 39780779
Thanks awking00 for your help and did not mean to violate any terms. I was able to solve my problem.
	public static void stripComments(Scanner input){
		// /* OR */
		boolean markerFound = false;
			String line = input.nextLine();
			if(markerFound == false && !line.contains("//") && !line.contains("/*") && !line.contains("*/")){
			}else {
				markerFound = true;
				}else if(!line.contains("/*") && line.endsWith("*/")){
					markerFound = false;
				}else if (line.contains("//")){
					int index = line.indexOf('/');
				}else {
					int index = line.indexOf("/*");
					int index2 = line.indexOf("*/");
					System.out.println(line.substring(0, index) + "" + line.substring(index2 + 2,line.length())	);

Open in new window


Author Comment

ID: 39783922
Thanks Netminder

I would like to know what would be appropriate way of asking my questions. I am not trying to violate any terms but I am self teaching myself java. I ordered the Building Java Programs book from Amazon and do not have the aid of a teacher but need to respect EE policies. The book uses the practice it website to aid in learning.

Should I state in my questions do not supply solution but point out my mistakes or something to that nature?

Author Comment

ID: 39785349
Okay, I understand now. I will post where the material comes from and mention that I want guidance not a solution.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 26
Java exception bubble up 2 17
sp_configure 'external scripts enabled', 1; 7 49
How to determine if a string is a valid SHA value 7 28
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question