0

So even though this is a simple project, I found a lack of good code on the web under this topic for multi lined strings. I thought I would share it with everyone. Hope it helps you! Prints file foward first, then in reverse (and yes there are many different ways to do this i.e. stringbuffer)

Please give feedback as to what you think...Thanks guys!

package reverseorama;

/**
 *
 * @author Jamesonh20
 * @version 9/17/10
 */
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileReader myReader;
        JFileChooser myChooser = new JFileChooser();
        if(myChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            File myFile = myChooser.getSelectedFile();
            try{
                myReader = new FileReader(myFile);
                Scanner myScanner = new Scanner(myReader);
                
                while (myScanner.hasNextLine()){
                    String textLine = myScanner.nextLine();
                    System.out.println(textLine);
                }                
                myReader.close();
                
                myReader = new FileReader(myFile);
                 myScanner = new Scanner(myReader);
                
                while (myScanner.hasNextLine()){
                    String textLine = myScanner.nextLine();                    
                    for(int i=textLine.length()-1; i>=0; i--)
                    {
                       System.out.print(textLine.charAt(i));
                    }
                    System.out.println();
                }                
                myReader.close();
            }
            catch (FileNotFoundException myEx) {
                System.out.println();
            }
            catch (IOException myEx){
                System.out.println(myEx.getMessage());
            }
        }
    }

}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jasimp
0

If you are going to provide code to be used as a learning tool it needs to be commented well.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.