I realize my problem is probably related to the general structure/organization of the code.
The issues I am having:
- try/catch from lines 81-94. I have commented it out so I could continue to work
- organization of methods and what to call in main - I feel that I am missing some basic understanding of how I SHOULD structure these properly.
Any advice/knowledge is appreciated. Thanks in advance!
import java.util.*;
import java.io.*;
public class Redaction
{
Scanner myScanner = new Scanner(System.in);
String inputString;
File outputFile;
String fileName;
public String Redaction()
{
//prompt for filename
System.out.println("Enter filename.txt: ");
fileName = myScanner.nextLine();
try
{
File inputFile = new File(fileName);
Scanner fileScanner = new Scanner(inputFile);
inputString = fileScanner.toString();
}
catch(FileNotFoundException e)
{
System.out.println("ERROR: " + e);
}
return inputString;
}
public String Redactor()
{
//prompt for filename
// System.out.println("Enter filename.txt: ");
// fileName = myScanner.nextLine();
try
{
File inputFile = new File(fileName);
Scanner fileScanner = new Scanner(inputFile);
inputString = fileScanner.toString();
}
catch(FileNotFoundException e)
{
System.out.println("ERROR: " + e);
}
//Prompt for myPattern to be replaced
System.out.println("Find: ");
String myPattern = myScanner.nextLine();
//Prompt for myReplacement to replace myPattern
System.out.println("Replace with: ");
String myReplacement = myScanner.nextLine();
//Create output string with replacements
String outputString = inputString.replaceAll(myPattern, myReplacement);
//Create new output.txt
String newFileName = fileName + "_redacted.txt";
outputFile = new File(newFileName);
try
{
PrintWriter myWriter = new PrintWriter(newFileName);
myWriter.write(outputString);
}
catch(FileNotFoundException e)
{
System.out.println("ERROR: " + e);
}
//http://www.mkyong.com/java/how-to-get-the-filepath-of-a-file-in-java/
// try
// {
String absolutePath = outputFile.getAbsolutePath();
System.out.println("File path : " + absolutePath);
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("File path : " + filePath);
// }
/* catch(IOException e)
{
e.printStackTrace(System.out);
}
*/
return outputString;
}
public static void main(String[] args)
{
Redaction myRedaction = new Redaction();
myRedaction.Redactor();
}
}

New Topic/Question
Reply



MultiQuote



|