1

I used reverse a string, but now need the final document is the principle, and vice versa:

Hello
Bye

to

Bye
hello

and not:

olleH
eyB

As I do this?

This is my source:

public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("Sintaxis incorrecta, introduzca el nombre del fichero");
        System.exit(1);
    }

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(args[0]));
        String s;

        try {
            while ((s = br.readLine()) != null) {
                StringBuilder reverse = new StringBuilder(s);
                String sCadenaInvertida = reverse.reverse().toString();
                System.out.println(sCadenaInvertida);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Thanks!!

Tiny
  • 27,221
  • 105
  • 339
  • 599
kaki
  • 21
  • 2
  • 2
    Please rephrase `but now need the final document is the principle, and vice versa:`. It doesn't make sense. – Sotirios Delimanolis Mar 31 '14 at 18:21
  • split into words, reverse the collection, join using space –  Mar 31 '14 at 18:22
  • 2
    You need to take the String, use `split` to get an array of the words, then make a new String, reading the words in descending order. Simple :D – AntonH Mar 31 '14 at 18:22
  • Before you post your question take a look at preview section to see if your question presents your problem correctly. I edited your question a little to show *how you wrote examples*, but I am not sure why some of upper case becomes lower case, so add more informations about input and expected output. – Pshemo Mar 31 '14 at 18:29
  • For now it looks like you need FILO like structure like stack, or just add each line to some list and after you are done with adding [read it backward](http://stackoverflow.com/a/2102552/1393766). – Pshemo Mar 31 '14 at 18:36

4 Answers4

2

Just put everything in an ArrayList and use Collections.reverse http://www.java2s.com/Code/Java/Collections-Data-Structure/ReverseorderofallelementsofJavaArrayList.htm

pseudo code:

    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("Hello");
    arrayList.add("Bye");

    Collections.reverse(arrayList);
    System.out.println(arrayList);
Scott
  • 1,648
  • 13
  • 21
  • That would only do it by lines, not words – Dan Mar 31 '14 at 18:34
  • agreed but it doesn't say anything about reversing words. I think they just want the lines in reverse order from the input. – Scott Mar 31 '14 at 18:39
  • Exactly, need to do what Scott says – kaki Mar 31 '14 at 18:45
  • need this: hello, my house is white bye, my mum is bad Bye, my mum is bad Hello, my house is white reverse order from document, and not reverse words. – kaki Mar 31 '14 at 18:49
  • @Dan Even if it were by words, the logic would still apply of using a list + Collections.reverse – Rogue Mar 31 '14 at 19:41
  • @user3482300 - The problem is that your original example is ambiguous. It ALSO shows the document being reversed by words. Your bad! – Stephen C Mar 31 '14 at 22:55
1

Add the items to an array (first come first serve) then traverse the array in reverse

for (into I = array.length; i >= 0; i--) {
    //print array[i]
}

Alternatively you can use an ArrayList if you don't know the number of lines in the document

Rogue
  • 11,105
  • 5
  • 45
  • 71
1
ArrayList<String> theWords= new ArrayList<String>();
while ((s = br.readLine()) != null) {
            //split line into words
            String[] parts = s.split("\\s+"):
            //for each word append to arraylist
            for(String s : parts)
            {
                 theWords.append(s);
            } //end for loop
 } //end while loop

// iterate array, from size-1 to 0
int theWordsSize = theWords.size()--;
for(int i= theWordsSize; i >= 0; i--)
{
     System.out.println(theWords.get(i));
 } //end for loop
Dan
  • 876
  • 5
  • 15
0

here the answer, It was easy:

public class Reverse2 {

public static void main(String[] args) {



        if(args.length != 1){
            System.err.println("Sintaxis incorrecta, introduzca el nombre del fichero");
            System.exit(1);
        }

        BufferedReader br = null;


        try {
            br = new BufferedReader(new FileReader(args[0]));

            ArrayList<String> lista = new ArrayList<String>();
            String s;

            try {
                while((s=br.readLine()) != null){
                    lista.add(s);

                }

                for(int i= lista.size()-1;i>=0;i--){
                    System.out.println(lista.get(i));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }





    }

}

thanks for all the possible solutions

kaki
  • 21
  • 2