I've got this project I'm doing for my cps class and I have set up a scanner and print writer and everything but I need to get rid of all whitespaces, comments, extra lines, and information in parenthesis. I have no clue how to go about doing this. My work is below Ive tried .replaceall and an if statement for each char but I've come to no avail. Thank you for your help.
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;
}
}
Aucun commentaire:
Enregistrer un commentaire