我正在尝试删除包含某个字符的代码行,但是我不知道该怎么做

时间:2020-02-12 16:44:58

标签: java

我正在尝试删除包含特定字符的代码行,但我不知道该怎么做。这是我的代码行和输出。我正在尝试删除!#之后的注释,例如(示例...程序)和#。还有空格。如果您想让我澄清一下,请告诉我。谢谢!

public class New {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String str = readFile("/Users/jalencarter/Desktop/snowflakeExample.txt");

        try (Scanner scanner = new Scanner(str)) {

            scanner.useDelimiter("([*'])|\\s+");

            while(scanner.hasNext()){
                System.out.println(scanner.next());
            }

        }  
    }    

    static String readFile(String Name) {    

        File fi = new File(Name);     
        char[] buffer = null;    
        try {    
            BufferedReader buff = new BufferedReader( new FileReader(fi));

            buffer = new char[(int)fi.length()];    
            int i = 0;    
            int j = buff.read();    
            while (j != -1) {    
                buffer[i++] = (char)j;    
               j = buff.read();   
            }    

        } catch (IOException e) {    
        }    

        return new String(buffer);
    }

}

输出

#!
Example
Snowflake
program
!#
parm
goat
cow;
star
=



;
while
goat
cow
{
#
repeat
unless
it
can
t
find
a
cow
goat
cow
=
star;
star
=
star



;
#
example
of
concatenation
}
return
goat;

1 个答案:

答案 0 :(得分:1)

我在这里遇到了一个陷阱:单行注释在下一个换行符处结束,但是扫描程序类无法区分空格和换行符。我不知道如何使用扫描仪类解决这个问题,但是我可以为您提供一个没有它的有效示例:

猜测的输入文件(test.txt):

#!
    Example Snowflake
    program
!#

parm goat cow;
star = ;
while goat cow
{
    # repeat unless it can't find a cow
    goat cow = star;
    star = star;
    # example of concatenation
}
return goat;

注释源代码:

import java.io.BufferedReader;
import java.io.FileReader;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        // Open the input file
        try (BufferedReader reader = new BufferedReader(new FileReader("test.txt")))
        {
            boolean isBlockComment = false;
            String line;

            // Repeat reading until there no more line
            while ((line = reader.readLine()) != null)
            {
                // Does a block comment start in this line?
                int posi = line.indexOf("#!");
                if (posi >= 0)
                {
                    isBlockComment = true;

                    // If there are no characters before the comment, skip the whole line
                    if (posi == 0)
                    {
                        continue; // skip output
                    }

                    // keep only the left part before comment
                    line = line.substring(0, posi);
                }

                // Does a block comment end in this line?
                posi = line.indexOf("!#");
                if (posi >= 0)
                {
                    isBlockComment = false;

                    // If there are no characters after the comment, skip the whole line
                    if (posi + 2 == line.length())
                    {
                        continue;
                    }

                    // keep only the right part after the comment
                    line = line.substring(posi + 2);
                }

                // Skip all lines within a block comment
                if (isBlockComment)
                {
                    continue;
                }

                // Does the line contain single-line comment?
                posi = line.indexOf("#");
                if (posi >= 0)
                {
                    // If there are no characters before the comment, skip the whole line
                    if (posi == 0)
                    {
                        continue;
                    }

                    // keep only the left part before comment
                    line = line.substring(0, posi);
                }

                // Until here, the comments are removed
                // test:
                // System.out.println(line);

                // The code below removes whitespaces

                // Remove whitespace at the beginning and end of the line
                line = line.trim();

                // Replace all multiple whitespaces by single whitespaces
                line = line.replaceAll("\\s{2,}", " ");

                // By using print instead of println, we remove line breaks
                System.out.print(line);

                // But we output a space after each line that is not empty
                if (!line.isEmpty())
                {
                    System.out.print(' ');
                }
            }
        }
    }
}

示例输出:

parm goat cow; star = ; while goat cow { goat cow = star; star = star; } return goat; 

如果要单独输出每个令牌(而不是逐行输出),则将下部更改为:

                // The code below removes whitespaces

                // Split the line at single or multiple whitespaces
                String tokens[] = line.split("\\s+");

                // Print only the tokens that are not empty
                for (int i=0; i<tokens.length; i++)
                {
                    if (! tokens[i].isBlank())
                    {
                        System.out.println(tokens[i]);
                    }
                }

那么输出将是:

parm
goat
cow;
star
=
;
while
goat
cow
{
goat
cow
=
star;
star
=
star;
}
return
goat;