Solved

Exercise 6.11: stripComments

Posted on 2014-01-10
11
968 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
  • 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 31

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now