Practice-It! logo

Exercise 6.11: stripComments

Added by: Jared Jones

NOTE: This problem seems to come from an oudated edition of the Building Java Programs textbook. Please make sure that you are solving the problem from the right edition of the textbook that you are using in your class. (Click here to jump to the current edition's version of this problem instead.)

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() {
        System.out.println("Goodbye!");
    }
}

You must log in before you can solve this problem.

user
Log In


indent Re-indent

If you do not understand how to solve a problem or why your solution code doesn't work, please contact your TA or instructor.

If something seems wrong with the Practice-It system itself (errors, slow performance, incorrect problem descriptions/tests, etc.), please contact us.

Is there a problem?
Contact a Practice-It administrator.