0

I'm currently attempting to write a program that can scan a text document and replace a specified word / string / whatever with another phrase, specifically using the classes Scanner and Printwriter. Unfortunately, I'm having a little bit of trouble finding the correct methods to use and how exactly to implement them. Here's my code:

class Redaction { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(System.in); System.out .println("Please enter the filename of the sensitive information"); String f = input.next(); System.out.println("Please input what text you want 'lost'"); String o = input.next(); System.out .println("Please input what you want the new, improved filename to be called"); String n = input.next(); File sensitiveDocument = new File(f); if (!sensitiveDocument.exists()) { System.out.println("File does not exist."); System.exit(0); } Scanner in = new Scanner(sensitiveDocument); in.useDelimiter("[^A-Za-z]+"); PrintWriter out = new PrintWriter(n); while (in.hasNext()) { if (in.hasNext(o)) { // ... } } in.close(); out.close(); } } 

I'm pretty lost at this point. Any help would be much appreciated.

2
  • But if (in.hasNext(o)) has no code in it. What are you confused about ? Commented Apr 11, 2013 at 6:23
  • Check out my answer here: Hope it will help! <code> stackoverflow.com/questions/7779265/… </code> Commented Oct 8, 2013 at 11:33

2 Answers 2

0

Start by reading PrintWriter and Scanner documentation, to decide which methods to use.

Pseodo code:

  1. Get line by line (or word by word, depends on what you want to remove).
  2. look for the string you want to remove
  3. if the string contains the content to remove, remove it.
  4. print the string to the file.
Sign up to request clarification or add additional context in comments.

Comments

0

The simplest although not so efficient algorithm would be to read the contents of the file into a string variable. After which you could use a String Tokenizer to find and replace the word you don't want with the word you want and rewriting the contents of the variable back into the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.