import java.io.*; public class Replace { private static BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) ); public static String processLine(String line) { String newLine = ""; String word = ""; char ch; for(int i = 0; i < line.length(); i++) { // ch is the current character ch = line.charAt(i); // if ch is a letter, append it to the word // being constructed if(Character.isLetter(ch)) word = word + ch; // otherwise, if ch is not a letter // and there is a word to output else if(word.length() != 0) { if(word.equals("red")) word = "blue"; newLine = newLine + word; word = ""; newLine = newLine + ch; } // otherwise, if ch is not a letter // and there is no word to output else newLine = newLine + ch; } if(word.length() != 0) { if(word.equals("red")) word = "blue"; newLine = newLine.concat(word); } return newLine; } public static void main(String [] args) { try { System.out.println("Please type the name of a file you want processed."); String filename = console.readLine(); FileReader fr = new FileReader(filename); BufferedReader stdin = new BufferedReader(fr); FileOutputStream outFile; outFile = new FileOutputStream(filename+".out"); PrintStream p = new PrintStream(outFile); String line = ""; line = stdin.readLine(); while(line != null) { String newLine = processLine(line); p.println(newLine); line = stdin.readLine(); } p.close(); } catch(java.io.IOException e) { System.out.println(e); } } }