Question
Java Problem Description: A well-written program will use comments judiciously to document the function and structure of a piece of high-level code. While this is
Java Problem Description:
A well-written program will use comments judiciously to document the function and structure of a piece of high-level code. While this is helpful, it might also be useful to have a tool that can read through a large code file and pull out all of the comments. This is where you come in.
Write a program in Java that prompts the user for a Java source file (for example, Hangman.java). Your program will create another file which uses the same name, but with the .txt file extension (example, Hangman.txt). In this output file, you will write only the comments, with line numbers.
Remember in Java, there are two types of comments:
Single line comments begin with // and include everything else on that line.
Multiline comments begin with /* and end with */.
The output should include only the comment text (including the punctuation /* and */ or //) but nothing else! Each line of output (in your text file) should be preceded with a line number and colon. See Required Output below for an example.
Create a robust solution by catching any I/O Exceptions that may occur during your program execution. As examples, the program should handle a missing file gracefully, or the situation in which you cannot create the output file (due to permissions, for example).
Notes:
A Multiline comment may begin and end in a single line.
/* This is my code */
A multiline comment may have Java code before and after it.
int total, /* Amount available */ used;
You should ignore any single line comments inside a multiline comment. /*****************************************
** This is my program // Weird comment! **
*****************************************/
The following situations do not need to be handled, but can be for extra credit:
int total, /* Amount available */ used /* Another comment */;
int total, /* Amount available */ used // Another comment
In the cases above, the first comment should always be picked up. For extra credit, pick up both comments, but not the intervening Java code.
Here are other weird ones. Not required, but can you properly handle them?
o System.out.println("No // comment here! ");
o System.out.println("No /* comment here! */");
o System.out.println("There is a" + // comment here! " comment. ");
Notes:
Do not create these classes in a package.
Turn in only your Java source files.
Required Main Class:
CScrape
Required Input:
Filename (Example: BrandName.java). Make sure that you accept the complete filename, including the .java extension. You should also accept filenames that include paths (e.g. ../testdata/CommentFile.java)
Required Output:
Your output should look something like the following example. It should include your name.
Comment Scrape J.Doe
1: /*****************************************
2: ** This is my program // Weird comment! **
3: *****************************************/
7: /* Amount available */
14: /* This is my code */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started