-
Replace data in file
I'm trying to write a program that will ask the user a question and then take the answer and put it into a file, replacing some existing text. I'm trying to use ReplaceAll but it's not the right solution. Below is what I have so far. Any help is greatly appreciated! :)
class ReplaceTest {
public static void main(String args[])
throws FileNotFoundException {
Scanner myScanner = new Scanner(System.in);
char replyA;
System.out.print("Did you water the garden today? (Y/N) ");
replyA = myScanner.findWithinHorizon(".",0).charAt(0);
PrintStream aTest = new PrintStream(new File ("yo.txt"));
String template = "here's a W";
aTest.println(template.replaceAll("W", replyA));
-
Besides posting your code, can you explain what the code is doing and why its not right and show what you'd like the code to do.
Also please post your code in code tags:
Java Forums - BB Code List
-
I'm trying to write a program that will take a user's response and replace some data in a text file, with that response. So far, this code is what I've got, but the error is happening because I can't use replyA in the replaceAll part
-
If you want to alter the contents of a file you need to read the file into memory, make changes, write contents back to file.
-
Quote:
the error is happening
What error?
Please copy and paste the full text of the error message here.