Write a method called stripComments that accepts a Scanner representing an input
ID: 3659149 • Letter: W
Question
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!"); } }Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class ProgramTest { public static void main(String rags[]) { ProgramTest programTest = new ProgramTest(); String strFileName = "help.txt"; try { Scanner scanner = new Scanner(new File(strFileName)); String[] strings2 = programTest.stripComments(scanner); programTest.displayProgram(strings2); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * This method is used to strip comments in the java class * * @param scanner * @return */ public String[] stripComments(Scanner scanner) { String[] strLines = new String[16]; int lineNumber = 0; while (scanner.hasNextLine()) { strLines[lineNumber] = scanner.nextLine(); ++lineNumber; } String[] res = new String[strLines.length]; int line = 0; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.