I have an assignment question that asks us, given a test phrase,

// the first comment
public class Test {
    // another comment
    public static void main( String[] args ) { // the main method
        String slashes = "//"; // ignore // in quotes
        System.out.println( slashes + " hi " + slashes );
    } // end of main
}

^ Test Phrase

We have to go through the code and remove all the 1 line comments. Taking what was given as an aid to the problem i modified to look like this

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;

public class RemoveLineComments {

    public static boolean skipPast( Reader rd, String end )
        throws IOException
    {
        int matched = 0;
        int ch;
        while( (ch=rd.read()) != -1 ) {
            char c = (char)ch;
            if ( c == end.charAt(matched) ) {
                matched++;
                if ( matched == end.length() ) {
                    return true;
                }
            }
            else {
                matched = 0;
            }
        }
        return false;
    }

    public static void main( String[] args ) throws IOException {
        if( args.length != 1 ) {
            System.out.println("usage: java RemoveComments infile");
            System.exit( 1 );
        }
        BufferedReader rd =
            new BufferedReader( new FileReader( args[0] ));
        int ch;
        while( (ch=rd.read()) != -1 ) {
            char c = (char)ch;
            if ( c == '/' ) {
                ch = rd.read();
                if ( ch == -1 ) {
                    System.out.print( '/' );
                    break;
                }
                c = (char)ch;
                if ( c == '/' ) {
                    if ( ! skipPast( rd, "  " ) ) break;
                }
                else {
                    System.out.print( '/' );
                    System.out.print( c );
                }
            }
            else {
                System.out.print( c );
            }
        }
        System.out.flush();
    }
}

However, i'm having some trouble making it look like i want it to. Right now i have the code so that it finds a double space and stops, So, my question is this, is there a way so that i will stop when it gets to the end of a line, and still print it properly

public class Test {

    public static void main( String[] args ) { // the main method
        String slashes = "//"; 
        System.out.println( slashes + " hi " + slashes );
    } 
}

^That's the proper way.

Recommended Answers

All 11 Replies

Can you post what the program's current output is?

Why does the "proper way" contain a comment on line 3?

and why do you allow your main method in the RemoveLineComments class to throw an Exception?

The comment on line 3 of the output is a typo, it should be like this

public class Test {
 
    public static void main( String[] args ) { 
        String slashes = "//"; 
        System.out.println( slashes + " hi " + slashes );
    } 
}

Currently, my output is this

public static void main( String[] args ) {       String slashes = "//";       System.out.println( slashes + " hi " + slashes );

All one line, and it even cut off the "public class Test" in the beginning, i'm telling it currently to stop at \n which is the beginning of a new line. I want it just to stop at the end of the line that the comment is on.

And the throwing the exception is so that if i don't load a file to remove the comments from, it returns the message, "No File Selected" (I've added that part in recently)

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;

public class RemoveLineComments {

    public static boolean skipPast( Reader rd, String end )
        throws IOException
    {
        int matched = 0;
        int ch;
        while( (ch=rd.read()) != -1 ) {
            char c = (char)ch;
            if ( c == end.charAt(matched) ) {
                matched++;
                if ( matched == end.length() ) {
                    return true;
                }
            }
            else {
                matched = 0;
            }
        }
        return false;
    }

    public static void main( String[] args ) throws IOException {
        if( args.length != 1 ) {
            System.out.println("No File Selected");
            System.exit( 1 );
        }
        BufferedReader rd =
            new BufferedReader( new FileReader( args[0] ));
        int ch;
        while( (ch=rd.read()) != -1 ) {
            char c = (char)ch;
            if ( c == '/' ) {
                ch = rd.read();
                if ( ch == -1 ) {
                    System.out.print( '/' );
                    break;
                }
                c = (char)ch;
                if ( c == '/' ) {
                    if ( ! skipPast( rd, "\n" ) ) break;
                }
                else {
                    System.out.print( '/' );
                    System.out.print( c );
                }
            }
            else if ( c == '"' ) {
		System.out.print( c );
		while( (ch=rd.read()) != -1 ) {
		    c = (char)ch;
		    System.out.print( c );
		    if ( ch == '"' ) break;
		}
		if ( ch == -1 ) break;
	    }
            else {
                System.out.print( c );
            }
        }
        System.out.flush();
    }
}

Undated Code.

Is the code doing what you want now?

No it still prints everything out in one line. I need to it stop at the end of the line, not when it starts the next one.

Is your problem knowing WHEN to print on the next line
Or HOW to print on the next line?
Here is how to print on the next line:
To have a String print on the next line, use println() on the last thing you want printed on the current line
Or print a "\n" character when you want the next String printed to go on the next line.

You'll have to work out the logic for when you want to do it.
Can you explain when you want to print on the next line?
What determines that?

what it it does is find the // and then remove everything after that, and then i want it to stop once it gets to the end of a line.

once it gets to the end of a line.

Can you tell when it gets to the end of a line?

No, that was my main problem, i don't know how to tell it to stop.

What methods are you using to read the lines? Do any of them return a value saying when you are at the end of a line?

What does the skipPast method do? I see it is passed a "\n". That is an end of line marker.
When the program reads the end of line character, you need to know it so you can call println to skip to a new line.


And the throwing the exception is so that if i don't load a file to remove the comments from, it returns the message, "No File Selected" (I've added that part in recently)

...
    public static void main( String[] args ) throws IOException {

what I ment to say was: you can't handle an Exception when your main method throws it. your main method is your entry point of your application, and, as being it "the root of all to be ;)" it's also the very last place where you can gracefully handle an Exception.

the way you've coded it, if an Exception is thrown, it'll be impossible for you to avoid a complete crash of your application. but, if you would do something like this:

public static void main(String[] args){
Scanner inp = new Scanner(Sysem.in);
try{
// your code that might throw the exception
}
catch(IOException ioe){
ioe.printStackTrace(); 
// not necessary, you can replace this by an error message of your own
System.out.println("***********************");
System.out.println("Do you want to terminate your program? (Y/N)");
String input = inp.next();
input = input.toLowerCase();
if ( 'y' == input.charAt(0))
  System.exit(0);
}
}

you can avoid the application to terminate if you want.
maybe all you needed was to store a default value in the variable you were reading, that too can be done in the catch block.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.