在Java中运行多个replaceAll语句

时间:2018-05-18 04:59:27

标签: java

这可能是一个非常简单的问题,但这是漫长的一天。 我正在尝试从java程序中删除注释。要做到这一点,我试图使用replaceAll,但我不知道如何同时运行该文件中的所有3个语句。除了正则表达式之外我还应该使用哪种方法吗?还是有一个简单的解决方案?这是我的代码:

import java.io.*;
import java.util.*;

public class Ex13Ch6 {
    public static void main(String args[]) throws FileNotFoundException {
        File file = new File("killComment.txt");
        Scanner s = new Scanner(file);

        stripComments(s);
    }
    public static void stripComments(Scanner s) {


        while(s.hasNext()) {

        String input = s.nextLine();

        String regex = "/*";

        // Removes /* from the file
        String change = input.replaceAll("/\\*", "");
        // Removes */ from the file
        String change2 = input.replaceAll("\\*/", "");
        // Removes // from the file
        String change3 = input.replaceAll("//", "");

        System.out.println(change2);
        }
    }
}

这是文本文件:

// Name: Conner Murowchick
// Email: conner.murowchick@bellevuecollege.edu
// Program description:
/*
Write a method called flipLines that accepts a Scanner for an input 
file and writes to the console the same file's contents with each
pair of lines reversed in order. If the file contains an odd number
of lines, leave the last line unmodified. 
*/ 

// importing file and Scanner
import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;
public class Ex7Ch6 {

public static void main(String[] args) throws FileNotFoundException{
// Calling the flipLines method
    flipLines();
}
public static void flipLines() throws FileNotFoundException {
// Calls a new file, "jabberwocky.txt"
    File file = new File("jabberwocky.txt");
// Scans the file "jabberwocky.txt" and creates a Scanner s to read it. 
    Scanner s  =  new Scanner(file);
// creates a new variable "noOfLines" that is an integer equal to 0
    int noOfLines = 0;
// A while loop that checks if there is another line to scan. 
    while(s.hasNextLine()) {
// If there is another line to scan, the scanner goes to the next line
        s.nextLine();
// The variable noOfLines increments while this loop runs.
        noOfLines++;
    }
// Creates a new scanner called s2 that reads the file "jabberwocky.txt"
        Scanner s2 = new Scanner(file);
// A for loop that runs when i is less than half of the variable noOflines
        for (int i=0; i < (noOfLines / 2); i++) {
// String called line1 that equals scanner s2 reading the next line of text
            String line1 = s2.nextLine();
// String called line2 that equals scanner s2 reading the next line of text
            String line2 = s2.nextLine();
// Prints the Second line first
            System.out.println(line2);
// Prints the First line second
            System.out.println(line1);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您的解决方案无法正常运行,因为它每次都会丢失更改。

尝试:

  // Removes /* from the file
  String change = input.replaceAll("/\\*", "");
  // Removes */ from the file
  String change2 = change.replaceAll("\\*/", "");
  // Removes // from the file
  String change3 = change2.replaceAll("//", "");

你甚至可以在一个声明中结束:

String change = input.replaceAll("/\\*", "").replaceAll("\\*/", "").replaceAll("//", "");

无论如何,这不会删除评论,它只会&#34;取消注释&#34;一些评论。正如user202729所建议的那样,您需要一个解析器来删除多行注释。对于注释的单行,您需要更具体的正则表达式,如:

"/^[\s]*[/]+.*/"