-3

I am trying to reverse the string. I am reading the string from a field, called abc.txt

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

public class MrText {

    private static final String NEW_LINE_SEPARATOR = System
            .getProperty("line.separator");

    public static void main(String[] args) throws IOException,
            ArrayIndexOutOfBoundsException {

        FileReader input = new FileReader("abc.txt");
        BufferedReader bufRead = new BufferedReader(input);

        StringBuffer rvsWords = new StringBuffer();
        String line;

        line = bufRead.readLine();

        while (line != null) {

            StringTokenizer tok = new StringTokenizer(line);

            String lineReversed = "";
            while (tok.hasMoreElements()) {

                String word = (String) tok.nextElement();

                for (int i = word.length() - 1; i >= 0; i--) {
                    rvsWords.append(word.charAt(i));

                }

            }

            line = bufRead.readLine();

            if (line != null) {
                rvsWords.append(NEW_LINE_SEPARATOR);
            }
        }
        bufRead.close();

        // File outFile = new File("gggggggggggggggggggg.txt");
        FileWriter writer = new FileWriter(outFile);
        writer.write(rvsWords.toString());
        writer.close();

        // System.out.println(rvsWords.toString());

        // rvsWords.setLength(0);
    }
}

text file for input: abc.text contains

IT comp mech civil

output by above code: TI pomc livic hcem

insted i wanyt the output: comp IT civil mech

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
  • You seem to have forgotten to ask a question – Holloway Oct 13 '14 at 08:45
  • So, you want to swap the order order (for two words) rather than reverse the letters? – doctorlove Oct 13 '14 at 08:45
  • Could you please format it a little better? – Vincent Beltman Oct 13 '14 at 08:46
  • possible duplicate of [Reverse the ordering of words in a string](http://stackoverflow.com/questions/1009160/reverse-the-ordering-of-words-in-a-string) – Joe Oct 13 '14 at 08:46
  • the above code does the following: for every word in the txt file, take the word as a string, scan it backward and put the char you encounter in another string. Then begin with the next word. Why should it print the output you expect? – lateralus Oct 13 '14 at 08:47
  • possible duplicate of [Reverse a string in Java](http://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Jens Oct 13 '14 at 08:47

5 Answers5

2

you don't need to change the order in which letters compose words: nextElement return you the next word. Then you apply this loop to this word :

for (int i= word.length()-1; i >=0 ; i--) 
{  
                rvsWords.append(word.charAt(i));

}  

This loop is causing the letters to be in reverse order. Thus don't use it.

Then just keep one element in a temp variable, in order to switch it with the next one.

Easy peasy

fifou
  • 65
  • 6
1
for (int charIndex= myString.length()-1; charIndex >=0 ; charIndex--) 
{  
                reversedString.append(originalString.charAt(charIndex));

}
mcr619619
  • 435
  • 1
  • 4
  • 13
1

You can also use StringUtils from the apache commons library.

It has many uses, and one of them is StringUtils.reverse("yourStringHere");

shepard23
  • 148
  • 2
  • 13
0

Using StringBuffer class you can easily reverse a string.

StringBuffer revString = new StringBuffer(word.toString());
System.out.println(revString.reverse().toString());
Mahesh
  • 1,063
  • 1
  • 12
  • 29
0

You did not need your for loop (it is reversing your words)

Take words directly and set them smartly in the output :

    StringBuffer rvsWords = new StringBuffer();
    String line;

    line = bufRead.readLine();

    int index = 0;

    while (line != null) {
        StringTokenizer tok = new StringTokenizer(line);

        while (tok.hasMoreElements()) {

            String word = (String) tok.nextElement();

            rvsWords.insert(index, " ");
            rvsWords.insert(index, word);

        }

        line = bufRead.readLine();

        if (line != null) {
            rvsWords.append(NEW_LINE_SEPARATOR);
        }

        index = rvsWords.length() - 1;
    }
yunandtidus
  • 3,847
  • 3
  • 29
  • 42