我已经为cps类完成了这个项目,并且已经设置了扫描仪和打印书写器以及所有其他东西,但我需要摆脱括号中的所有空格,注释,多余的行和信息。我不知道如何去做。我的工作是在下面,我尝试过.replaceall和每个字符的if语句,但是我没有用。谢谢您的帮助。
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;
}
}