3 Replies - 407 Views - Last Post: 15 November 2012 - 10:59 AM Rate Topic: -----

#1 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Find and Replace for .txt docs.

Posted 15 November 2012 - 10:10 AM

Hello DICs, java noob again. I am working on a program that scans a document, finds and replaces a given string, and writes it to a new .txt

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();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Find and Replace for .txt docs.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Find and Replace for .txt docs.

Posted 15 November 2012 - 10:34 AM

Where is your loop reading replacing ?

      while(fileCan.hasNextLine()) {
        String line = fileScan.nextLine();
        line.replace(myPattern, myReplacement);
        myWriter.write(line);
        myWruite.write("\n");
      }


Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Find and Replace for .txt docs.

Posted 15 November 2012 - 10:56 AM

I would like to keep the System.in stream out of your Redaction class
Let another class or main handle the user inputs

You seem to be having trouble solving your problem because you do not break it down into smaller steps
Define some methods that only have simple tasks, it will make it a lot easier for you I bet
public class Redaction {
	private String content;
	
	public Redaction(File file) {
		content = getAllText(file);
	}
	
	public void replace(String regex, String replacement, File output) {

	}
	
	private void writeToFile(File file, String text) {
		//Write 'text' to the given file
	}
	
	private String getAllText(File file) {
		//Read all the text from the file
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Find and Replace for .txt docs.

Posted 15 November 2012 - 10:59 AM

Does this part not work for that?

String outputString = inputString.replaceAll(myPattern, myReplacement);


Also, I can't even get it to prompt me to input a filename for it to scan in the first place

CasiOo -

I will give that a shot, and get back. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1