Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exercise 6.11: stripComments

Posted on 2014-01-10
11
Medium Priority
?
1,122 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 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

704 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