Results 1 to 4 of 4
  1. #1
    tranc3nd is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Replace Each line of a file with its reverse.. Please Help..

    Hi here is the question..

    "Write a program that replaces each line of a file with its reverse. For example, if you run"

    "java Reverse HelloPrinter.java" this is typed through Command Prompt
    "then the contents of HelloPrinter.java are changed to"

    retnirPolleH ssalc cilbup

    {
    )sgra ][gnirtS(niam diov citats cilbup

    {
    wodniw elosnoc eht ni gniteerg a yalpsiD //

    ;)"!dlroW ,olleH"(nltnirp.tuo.metsyS

    }

    }
    I do not know how to begin with this problem. Any help would be appreciated. I am very new in Java.. Thanks again.
    Last edited by tranc3nd; 05-08-2012 at 03:07 PM.

  2. #2
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Replace Each line of a file with its reverse.. Please Help..

    Start by breaking it down in smaller steps. The first step should be learning how to read a file and assign its contents to a String, String array or whatever. The tutorials and Google will help you with that. Then post your best efforts and ask specific questions about it.

  3. #3
    tranc3nd is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Replace Each line of a file with its reverse.. Please Help..

    Okay thanks for the tip CustomException. I will go ahead and look for more tutorials in google.. I will post the program if i run into any problems. Thanks again.

  4. #4
    tranc3nd is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Replace Each line of a file with its reverse.. Please Help..

    Okay this is what i have so far on my code

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Iterator;

    public class ReverseLines {

    public static void main(String[] args) {
    try {
    BufferedReader input = new BufferedReader(new FileReader(args[0]));
    ArrayList list = new ArrayList();
    String line;
    while ((line = input.readLine()) != null) {
    list.add(line);
    }
    input.close();


    PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
    for (Iterator i = list.iterator();i.hasNext(); --i) {
    output.println((String)i.next());

    }
    output.close();
    } catch (IOException e) {
    System.err.println(e);
    }
    }
    }
    Now i have problem reversing the output file using the for loop. How can i do that?

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2012, 11:47 AM
  2. Replies: 14
    Last Post: 04-26-2011, 02:18 AM
  3. Replies: 8
    Last Post: 12-28-2010, 02:24 PM
  4. strings stringbuilders replace and reverse
    By bicepatron in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 01:17 PM
  5. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 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
  •