Description: Delete all comments for a valid Java file, with a comment "//" or "/* * * * *" indicated.
Note: text in single or double quotation marks may include "//" or "/* * * * * *", but not part of the comment.
Solution: After reading a file, a character is processed in one character.
1 Public voidDeleteallcomment (InputStream is)throwsIOException {2 inti =Is.read ();3 while(I! =-1) {4 Charc = (Char) I;5 if(c = = '/')) {6 Charj = (Char) Is.read ();7 if(j = = '/')) {8 Deletethisline (IS);9}Else if(j = = ' * ') {Ten Deleteuntilend (IS); One } A}Else if(c = = ' \ ' | | c = = ' "')) { - echostring (is, c); -}Else { the System.out.print (c); - } -i =Is.read (); - } + } - + //Delete to "//"Note at the beginning of the A Private voidDeletethisline (InputStream is)throwsIOException { at Chari = (Char) Is.read (); - Charj = (Char) Is.read (); - while(true) { - if(i = = ' \ r ' && j== ' \ n ')) { - Break; -}Else { ini =J; -j = (Char) Is.read (); to } + } - } the * //Delete comments that are denoted by "/* */" $ Private voidDeleteuntilend (InputStream is)throwsIOException {Panax Notoginseng Chari = (Char) Is.read (); - Charj = (Char) Is.read (); the while(true) { + if(i = = ' * ' && j== '/')) { A Break; the}Else { +i =J; -j = (Char) Is.read (); $ } $ } - } - the //output the contents of single or double quotes as-is - Private voidEchostring (InputStream is,intStartthrowsIOException {Wuyi CharEnd = (Char) Is.read (); the while(End! =start) { - System.out.print (end); WuEnd = (Char) Is.read (); - } About}
Microsoft Internship Interview-Delete all comments for a Java file