-4

i have to reverse a passage of text in java. For example if the text file had

Hello world
This is a example

and you run java reverse input.txt output.txt, then output.txt contains

This is a example
Hello world

the code I have so far only reads the input. txt in the standard way. Any ideas on how to reverse. Im stuck

import java.io.*;

public class Test {
    public static void main(String [] args) {

        /**
         *  The name of the file and the location of the file.                  
         */
        String fileName = "test.txt";


        /**
         * This will enter one line at a time
         * FileReader reads text files
         */
            String line = null;

            try {
            FileReader fileReader = 
            new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }    

            /**
             * Closing the file.
             */
            bufferedReader.close();  


            }
            /**
             *Telling the program what message to show if the file is not found
             */

            catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
            catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                   


        }
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
James
  • 1
  • 1
  • 3
  • 6
    just use a simple stack of string type! – ConductedClever Feb 13 '15 at 15:53
  • I think it helps to think about the problems in the real world before trying to code them. Write a list of words on a piece of paper. Try to reverse the order of words but only reading them from top down. When you've worked that out, there's a good chance you'll have created something similar to a stack. Then try to recreate the process using Java. – AdamMc331 Feb 13 '15 at 16:09

2 Answers2

1

You can use a stack of string type. add the strings to stack while reading the file:

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            myStack.Push(line);
        }

and read from stack at end :

bufferedReader.close(); 
while(!myStack.isEmpty()){
    System.out.println(myStack.Pop());
}

and some little changes on code.

ConductedClever
  • 4,175
  • 2
  • 35
  • 69
0

You can use Recursion:

//firt you need have a method by allowing it to call itself
void tac (){
  /*local variable, change betweens calls because the language have his
    own stack that use for make calls at the methods*/
  String line;
  //end condition;
  if (line = bufferedReader.readLine()) != null){
     //new instance  of method 
     tac ();
     //print the value of his local variable instance
     System.out.println(line);
  }
}

the first call use line("1") and call itself and make a new line ->line("2") this can continue ... line("N")
when it get the end condition; Return in inverse order of calling, them print line("N") this return...., print line("2") this return and print line("1")