Results 1 to 5 of 5
  1. #1
    annepiper is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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));

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,771
    Rep Power
    25

    Default

    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

  3. #3
    annepiper is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,771
    Rep Power
    25

    Default

    the error is happening
    What error?

    Please copy and paste the full text of the error message here.

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2011, 03:36 PM
  2. Find and Replace in Text File
    By hamidsharifi in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 03:01 AM
  3. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 03:52 AM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  5. Replies: 2
    Last Post: 08-18-2008, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •