Solved

Exercise 6.11: stripComments

Posted on 2014-01-10
11
1,101 Views
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() {
        System.out.println("Goodbye!");
    }
}

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 = "";
    while(input.hasNextLine()){
        line = input.nextLine();
        if(line.contains("/*") && !line.contains("*/" )){
            
            System.out.println();
           
        }else if( line.contains("*/")){
            
        }else if(line.contains("//") ){
            int index = line.indexOf("//");
            System.out.println(line.substring(index-index, index));
        }
        else{
            System.out.println(line);
        }
        
        
    }
    
}

Open in new window

1
Comment
Question by:DOCDGA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
11 Comments
 
LVL 32

Assisted Solution

by:phoffric
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.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 39773716
The file I attached writes the text without comments to the console and also creates a NewProgram.java 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.
0
 

Author Closing Comment

by:DOCDGA
ID: 39776730
Thanks awking00 and phoffric

awking00, I don't see the attachment
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 32

Expert Comment

by:awking00
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.
0
 

Author Comment

by:DOCDGA
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){
		//FLAG TO CHECK IF MARKER WAS FOUND
		// /* OR */
		boolean markerFound = false;
		
		while(input.hasNextLine()){
			//PLACE LINE IN VARIABLE
			String line = input.nextLine();
			
			if(markerFound == false && !line.contains("//") && !line.contains("/*") && !line.contains("*/")){
									System.out.println(line);					
			}else {
				if(line.startsWith("/*")){ 
				System.out.print("");
				markerFound = true;
				}else if(!line.contains("/*") && line.endsWith("*/")){
					System.out.println();
					markerFound = false;
				}else if (line.contains("//")){
					int index = line.indexOf('/');
					System.out.println(line.substring(0,index));
				}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

0
 

Author Comment

by:DOCDGA
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?
0
 

Author Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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