Solved

Exercise 6.11: stripComments

Posted on 2014-01-10
11
1,026 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 62
How to determine if a string is a valid SHA value 7 35
sql import cannot be resolved jsp 3 44
Indy 10 not Receiving UDP broadcast 3 21
A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

789 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