java - 如何取出一行文本并取出所有注释、空行、多余的空格和括号中的信息?

标签 java printwriter

这个项目要求我通过取出所有注释、空行、额外的空格和括号中的信息来读取处理中的文件,然后将其打印到output.txt中。我在处理数据和删除所有注释、空行、额外的空格和括号中的信息时遇到了麻烦。

这是我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

/*
 * @author wrigh4d
 * Date: 11/7/19
 * Description:  Takes instruction from SumN and then prints it into output.txt 
 */

public class InstructionExtractor {

    public static void main(String[] args) throws FileNotFoundException {
        //Sets up Scanner/Print Writer and declares variable
        File myFile = new File("sumN.txt");
        Scanner sc = new Scanner(myFile);

        File outputFile = new File("output.txt");
        PrintWriter pw = new PrintWriter(outputFile);

        String currentLine = "";

        //Sets up while loop to read in each line
        while(sc.hasNextLine()) {
            currentLine = sc.nextLine();    //Sets each line to currentLine
            currentLine = processLine(currentLine); //Takes currentLine down to processLine
            if(currentLine != "") { //if there is something inside of current Line it appends it to printwriter
                pw.append(currentLine + "\n");
            }

        }

        pw.flush(); //prints onto output.txt

    }

    public static String processLine(String currentLine) {
        //Gets rid of whitespace inside of currentLine
        String a = currentLine.replaceAll("\\s+","");
        System.out.println(a);

        return a;

    }

}

这是 SumN:

// Adds 1+...+100.
//initialize variable i and sum
@i // i refers to some mem. location.
M=1 // i=1
@sum // sum refers to some mem. location.
M=0 // sum=0

//the loop to sum
(LOOP)  //the LOOP label
@i
D=M // D=i
@100
D=D-A // D=i-100
@END
D;JGT // If (i-100)>0 goto END
@i
D=M // D=i
@sum
M=D+M // sum=sum+i
@i
M=M+1 // i=i+1
@LOOP
0;JMP // Goto LOOP

//Terminate the program
(END)
@END
0;JMP // Infinite loop

最佳答案

您可以使用Regex Expression匹配您要取出的东西:

public static String processLine(String currentLine) {
    // Gets rid of whitespace inside of currentLine
    String a = currentLine.replaceAll("//.+|\\s|\\(.+", "");
    System.out.println(a);

    return a;
}

同时使用 isEmpty() 函数评估 currentLine,而不是 != "":

// Sets up while loop to read in each line
while (sc.hasNextLine()) {
    currentLine = sc.nextLine(); // Sets each line to currentLine
    currentLine = processLine(currentLine); // Takes currentLine down to processLine
        if (!currentLine.isEmpty()) { // if there is something inside of current Line it appends it to printwriter
            pw.append(currentLine + "\n");
    }
}

关于java - 如何取出一行文本并取出所有注释、空行、多余的空格和括号中的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925478/

相关文章:

java - 在 setVisible 后创建 JComboBox 使 JButton 消失

java - 如何从Android动态生成的表格中获得点击时的列号?

java - 应用程序从 webapp tomcat7 写入文件

java - 为什么本地 PrintWriter 会干扰另一个本地 PrintWriter?

java - Maven 本地 Artifact 解析在 1 天后不起作用

java - 如何在java数据报包中发送字符串数组?

servlets - HttpServletResponse PrintWriter 写入输入流

java - BufferedReader 在 readLine() 上阻塞

java,以txt文件的形式输出不正确(简单的从控制台输入两个值并输出相加的值)

java - 带有图像背景和文本对齐的 JButton