Avatar of DOCDGA
DOCDGA
 asked on

Exercise 6.11: stripComments

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

ProgrammingJavaC++

Avatar of undefined
Last Comment
DOCDGA

8/22/2022 - Mon
SOLUTION
phoffric

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
DOCDGA

ASKER
Thanks awking00 and phoffric

awking00, I don't see the attachment
awking00

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.
DOCDGA

ASKER
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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
DOCDGA

ASKER
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?
DOCDGA

ASKER
Okay, I understand now. I will post where the material comes from and mention that I want guidance not a solution.