2

How do I reverse my array output? Like "peter" to "retep" or "max" to "xam"

I tried to use collections but it's not working

This is my code:

import java.util.Scanner;
import java.util.Collections;

public class sdf {

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        String[] my_friend_names = new String[10];

        for (int i = 0; i < my_friend_names.length; i++) {
            my_friend_names[i] = input.nextLine();
        }
        for(int i = 0; i < my_friend_names.length; i++) {
            System.out.println("Name: " + my_friend_names[i]);
        }

        Collections.reverse(input);
        System.out.println("After Reverse order: " +input);
    }

}
3
  • To clarify. You want to reverse the order of the items in the array? Or reverse the order of the letters in the items? Commented Nov 17, 2014 at 16:02
  • Welcome to Stack Overflow (SO). Please read How to Ask Commented Nov 17, 2014 at 16:04
  • The bottom line is that the source code you've provided doesn't harmonize with question you're asking. I don't think you understand the problem you are trying to solve Commented Nov 17, 2014 at 16:04

4 Answers 4

3

Seems you create a string array, but than proceed to try reverse the input. If you want to use collections you may do something like this:

    List<String> list = Arrays.asList(my_friend_names);
    Collections.reverse(list);
    System.out.println("After Reverse order: " + list);
Sign up to request clarification or add additional context in comments.

Comments

1

Your posted code does not compile, for example you call Collections.reverse() on your scanner variable.

Things that might help you.

  • You've assumed Collections.reverse() will reverse the Strings within the array - it won't, it simply reverses the order of the Strings, e.g.
  • Collections.reverse() works on java.util.List not a primitive array, you can use Arrays.toList() if you need it
  • StringBuilder provides a handle reverse() method

Example, use StringBuilder.reverse() to update replace each item in the array with a reversed String

String[] my_friend_names = { "fred", "alice" };

for (int i = 0; i < my_friend_names.length; i++) {
    my_friend_names[i] = new StringBuilder(my_friend_names[i])
            .reverse().toString();
}

System.out.println(Arrays.toString(my_friend_names));

Output

[derf, ecila]

Comments

0

You are trying to print out input which is of type Scanner. Try using StringBuilder

Scanner input = new Scanner(System.in);
String[] my_friend_names = new String[10];

for (int i = 0; i < my_friend_names.length; i++) {
    my_friend_names[i] = input.nextLine();

    String reversedName =  new StringBuilder(my_friend_names[i]).reverse().toString();
    System.out.println("After reverse: " + reversedName);
}

Comments

0

I think it would be the best way to make a char[] from the String, reverse that char[] and convert it back to a String.

Something like this should do the job:

    Scanner input = new Scanner(System.in);
    String[] my_friend_names = new String[10];

    for (int i = 0; i < my_friend_names.length; i++) {
        my_friend_names[i] = input.nextLine();
    }

    input.close();

    for(String name : my_friend_names) {
        System.out.println("Name: " + name);
    }

    for(int i=0; i<my_friend_names.length; i++) {
        char[] characters=my_friend_names[i].toCharArray();
        List<char[]> reverse=Arrays.asList(characters);
        Collections.reverse(reverse);
        my_friend_names[i]=new String(characters);
    }

    System.out.println("After Reverse order: ");

    for(String name : my_friend_names) {
        System.out.println("Name: " + name);
    }

Let me know whether it works (or not)
Happy coding :) -Charlie

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.