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); } }}
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.
awking00, I don't see the attachment