it works but instead of it reverseing the characters i want it to do this "This is a test.” should be reversed to read: “test. a is This”
i want it to do all this in a text file

import java.io.*;
import java.util.*;

public class reverse{
    
    public static void main(String[] args) {
        
        try {
            File f = new File("backwards.txt");
            FileReader fr = new FileReader(f);
            
            char[] c = new char[(int)f.length()];
            char[] cnew = new char[(int)f.length()];
            StringBuffer sbuf = new StringBuffer();
            fr.read(c,0,(int)f.length());
            
            int len = (int)f.length();
            
            for (int i = 0, j = len - 1; i < len ; i++, j--) {
                cnew[i] = c[j];
                sbuf.append(cnew[i]);
            }
            
            System.out.println(sbuf.toString());
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Recommended Answers

All 8 Replies

I'm not familiar with the Filereader class or Exception handling, but why don't you try to compile and run this?

import java.io.*;
import java.util.*;

public class reverse
{  
  public static void main(String[] args) throws Exception
  {  
    File f = new File("backwards.txt");
    File f1 = new File("reversed.txt");
    Scanner finput = new Scanner(f);
    PrintWriter pw = new PrintWriter(f1);
    
    String sentence = "", s = "", r = ""; // the strings I will need.
    
    while(finput.hasNext()) { sentence = s + finput.nextLine();} // read the input
    
    String[] toks = sentence.split(" "); // split the input into an array of words instead of an array of characters.
    
    for(int i = toks.length - 1 ; i >= 0; i--) { r = r + toks[i] + " ";} // reorder them in the order you like.
    
    pw.println(r); // print to the output file.
    pw.close();  // close the PrintWriter object.
  }
}

If this helps you out would you mind giving me a +up?

Thanks & Good Luck!

how would you print it out instead of creating a new file and it seems to get stuck on a comma when reading it

Oh so you don't want a new file, well all you need to do is comment out or delete the printwriter object, and print to the system instead.

import java.io.*;
import java.util.*;

public class reverse
{  
  public static void main(String[] args) throws Exception
  {  
    File f = new File("backwards.txt");
    //File f1 = new File("reversed.txt");
    Scanner finput = new Scanner(f);
    //PrintWriter pw = new PrintWriter(f1);
    
    String sentence = "", s = "", r = ""; // the strings I will need.
    
    while(finput.hasNext()) { sentence = s + finput.nextLine();} // read the input
    
    String[] toks = sentence.split(" "); // split the input into an array of words instead of an array of characters.
    
    for(int i = toks.length - 1 ; i >= 0; i--) { r = r + toks[i] + " ";} // reorder them in the order you like.
    
System.out.println(r); // This should print it out to your computer screen instead.  
    //pw.println(r); // print to the output file.
   // pw.close();  // close the PrintWriter object.
  }
}

This should do the trick. If it does do you mind giving me an up vote? It's a button at the top right hand corner of each post. And don't forget to mark the thread as solved. Thanks!

that works but how would it work to read multiple lines of a text file and print it out.

it works great but how would it work to do multiple lines instead of a single line, and then print them out.

it works great but how would it work to do multiple lines instead of a single line, and then print them out.

The program does that already.

while(finput.hasNext()) { sentence = s + finput.nextLine();} // read the input

The method hasNext() it true as long as there is a a next line in the input file. So the code block following the while condition excutes until there is no longer any data to read. Try and add a few paragraphs to the backwards.txt file and recompile the program, you'll see that it still works.

Hey!
I checked out the code again and consulted the API. The scanner is throwing an exception some where between paragraphs. Off the top of my head I'm not sure how to handle this, but I'll look into it and get back to you… A try catch block might resolve the problem, so if your good with those maybe you'll figure it out before me!

when i run the program all it prints out is
Four score and seven years ago our fathers brought forth on this continent a new


(This is my backwards.text)
earth.
the from perish not shall people, the for people, the by people, the of government
that freedom—and of birth new a have shall God, under nation, this vain—that
in died have not shall dead these that resolve highly here we devotion—that of
measure full last the gave they which for cause that to devotion increased take we
dead honored these from us—that before remaining task great the to dedicated here be
to us for rather is It advanced. nobly so far thus have here fought who they which
work unfinished the to here dedicated be to rather, living, the us for is It here.
did they what forget never can it but here, say we what remember long nor note,
little will world The detract. or add to power poor our above far it, consecrated
have here, struggled who dead, and living men, brave The ground. this hallow
not can consecrate...we not can dedicate...we not can we sense, larger a in But,

this. do should we that proper and fitting altogether
is It live. might nation that that lives their gave here who those for place
resting final a as field, that of portion a dedicate to come have We war. that of
battle-field great a on met are We endure. long can dedicated, so and conceived so
nation, any or nation, that whether testing war, civil great a in engaged are we Now

equal. created
are men all that proposition the to dedicated and Liberty, in conceived nation,
new a continent this on forth brought fathers our ago years seven and score Four

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.