Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to create a method that brings up a .txt file with lines of code in

ID: 3836555 • Letter: I

Question

I am trying to create a method that brings up a .txt file with lines of code in it and filters out the method. It's for class so we haven't learned a lot of the newer stuff. I feel like I did it, except for some reason it isn't working. Could anybody help maybe fix this code? the HW4Q4P1.txt is in the projects folder and everything

import java.io.*;

import java.util.*;

public class stripComment {

  

       public static void main(String[] args)

      

               throws FileNotFoundException{

  

           File RGIII = new File("HW4Q4P1.txt");

          

           Scanner Genesis = new Scanner(RGIII);

  

           stripComments(Genesis);

          

       }

           public static void stripComments(Scanner read_input)

           {

              

              String read_line = "";

              while(read_input.hasNextLine())

              {

              read_line = read_input.nextLine();

              if(read_line.contains("/*")&&!read_line.contains("*/"))

              {

              System.out.println();

              }

           else if( read_line.contains("*/"))

              {}

           else if(read_line.contains("//") )

              {

              int idx = read_line.indexOf("//");

           System.out.println(read_line.substring(0, idx));

              }

              else

              {

              System.out.println(read_line);

              }

              }

          

       }

      

}

Explanation / Answer

//stripComment.java
import java.io.*;
import java.util.*;

/**
* @author
*
*/
public class stripComment {

   /**
   * @param args
   * @throws FileNotFoundException
   */
   public static void main(String[] args)

   throws FileNotFoundException {

       File RGIII = new File("HW4Q4P1.txt");

       Scanner Genesis = new Scanner(RGIII);

       stripComments(Genesis);

   }

   /**
   * @param read_input
   */
   public static void stripComments(Scanner read_input)
           throws FileNotFoundException

   {

       String read_line = "";
       PrintWriter printWriter = new PrintWriter(new File("a.txt"));

       while (read_input.hasNextLine())

       {

           read_line = read_input.nextLine();

           if (read_line.contains("/*") && !read_line.contains("*/"))

           {

               System.out.println();

           }

           else if (read_line.contains("*/"))

           {
           }

           else if (read_line.contains("//"))

           {

               int idx = read_line.indexOf("//");
               String str = read_line.substring(0, idx);
               System.out.println(str);
               printWriter.write(str + " ");

           }

           else

           {

               System.out.println(read_line);
               printWriter.write(read_line + " ");

           }

       }

       printWriter.flush();
       printWriter.close();
   }

}

HW4Q4P1.txt
import java.io.*;
import java.util.*;

/**
* @author
*
*/
public class stripComment {

   /**
   * @param args
   * @throws FileNotFoundException
   */
   public static void main(String[] args)

   throws FileNotFoundException {

       File RGIII = new File("HW4Q4P1.txt");

       Scanner Genesis = new Scanner(RGIII);

       stripComments(Genesis);

   }

   /**
   * @param read_input
   */
   public static void stripComments(Scanner read_input)
           throws FileNotFoundException

   {

       String read_line = "";
       PrintWriter printWriter = new PrintWriter(new File("a.txt"));

       while (read_input.hasNextLine())

       {

           read_line = read_input.nextLine();

           if (read_line.contains("/*") && !read_line.contains("*/"))

           {

               System.out.println();

           }

           else if (read_line.contains("*/"))

           {
           }

           else if (read_line.contains("//"))

           {

               int idx = read_line.indexOf("//");
               String str = read_line.substring(0, idx);
               System.out.println(str);
               printWriter.write(str + " ");

           }

           else

           {

               System.out.println(read_line);
               printWriter.write(read_line + " ");

           }

       }

       printWriter.flush();
       printWriter.close();
   }

}
a.txt
import java.io.*;
import java.util.*;

* @author
*
public class stripComment {

   * @param args
   * @throws FileNotFoundException
   public static void main(String[] args)

   throws FileNotFoundException {

       File RGIII = new File("HW4Q4P1.txt");

       Scanner Genesis = new Scanner(RGIII);

       stripComments(Genesis);

   }

   * @param read_input
   public static void stripComments(Scanner read_input)

   {

       String read_line = "";

       while (read_input.hasNextLine())

       {

           read_line = read_input.nextLine();


           {

               System.out.println();

           }


           {
           }

           else if (read_line.contains("

           {

               int idx = read_line.indexOf("

               System.out.println(read_line.substring(0, idx));

           }

           else

           {

               System.out.println(read_line);

           }

       }

   }

}

Note: According to the logic given by you, i write the content to the a.txt file, let me know if anything

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote