-4

I'm new to java and wanted to reverse an array the user inputs (including character) so apple, banana becomes ananab, elppa. But it outputs [null]

package test;

import java.util.Arrays;
import java.util.Collections;

import javax.swing.JOptionPane;

public class WhileWarmup{

    public static void main(String[] args) {
        String shoppinglist = JOptionPane.showInputDialog("What do you want to manipulate");
        String[]Shoppinglist = shoppinglist.split(",");
        String[]Shoppinglist1 = shoppinglist.split(",");
        
        int num = Shoppinglist.length;
        String bruh = "";
        String bruh1 = "";
        while (num >= 0) {
            Shoppinglist1 = Arrays.copyOfRange(Shoppinglist,num,num + 1);
            Collections.reverse(Arrays.asList(Shoppinglist1));
            bruh1 =  Arrays.toString(Shoppinglist1);
            bruh = bruh + bruh1;
            num = num - 1;
            System.out.println(bruh);
        }
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Did you try debugging your code? It seems to me that `Arrays.copyOfRange` doesn't do what you think it does. – Amongalen Aug 19 '20 at 11:11
  • Does this answer your question? [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Amongalen Aug 19 '20 at 11:12
  • `Arrays.copyOfRange` is a sub array right – William Greenfield Aug 19 '20 at 11:15
  • 2
    Also note: you are very much overcomplicating your own life. The problem you want to understand: how to fully reverse some array of strings. Then write code that just does that: trying to reverse a known HARDCODED array of strings. You see, all that Swing UI stuff, and the time spent for TYPING in your values ... that does not relate to your underlying problem AT ALL. Remember: reduce your problem to its core, and get rid of anything that does not contribute to it. Then you wouldnt need to give your examples in the question title. Then your code would tell us what you did. – GhostCat Aug 19 '20 at 11:28
  • 2
    And: learn about java naming conventions. variable names go camelCase, always. So "Shopplinglist" rather sounds like the name of some class. And of course. the real answer is: dont just blindly use some library method. Read the corresponding java doc to understand what it is supposed to do – GhostCat Aug 19 '20 at 11:29
  • 2
    Finally: what did you expect? The person whose name I shall not say that you are using in your example ... basically resembles an empty void. It isnt surprising that reversing that results in null. – GhostCat Aug 19 '20 at 11:30
  • Please [edit] your question. It is **entirely** unclear what you want to achieve. Do you want to reverse each `String` in an array or only the array itself but leave the items alone?? – Lino Aug 19 '20 at 11:54
  • Downvoted: we asked you repeatedly to improve your question to make it answerable. You expect other people to spend their free time to help you with your problem. So you please spend the time needed to improve your content. – GhostCat Aug 19 '20 at 13:39
  • What I understand from your ques is you want to reverse the array with reversing each array element. {hello,"trump"} will be {"pmurt","olleh"} - Doesn't it reverse the string of the array? Not understanding why every answer is downvoted when people have answered correctly. – garima garg Aug 19 '20 at 13:39

4 Answers4

1

When it comes to reversing a String, StringBuilder#reverse is always there:

    public static void main(String[] args) {
        List<String> wordList = Arrays.asList("hello", "Donald", "Trump");
        System.out.println(reverseListAndItsElements(wordList));
    }

    public static List<String> reverseListAndItsElements(List<String> list) {
        List<String> reversed = new ArrayList<>(list);//Create a copy
        Collections.reverse(reversed);
        return reversed.stream().map(s -> new StringBuilder(s).reverse().toString())
            .collect(Collectors.toList());
    }

Prints:

[pmurT, dlanoD, olleh]

And in case you do not want to mess with lists:

public static void main(String[] args) {
    String original = "hello,Donald,Trump";
    String[] split = original.split(",");
    String[] reversedSplit = new String[split.length];
    IntStream.range(0, split.length).forEach(index -> {
        reversedSplit[index] = new StringBuilder(split[index]).reverse().toString();
    });
    System.out.println(Arrays.toString(reversedSplit));
}
George Z.
  • 6,643
  • 4
  • 27
  • 47
0

Try this.

String[] input = {"hello", "Donald", "Trump"};
String[] reversed = new StringBuilder(String.join(",", input))
    .reverse().toString().split(",");
System.out.println(Arrays.toString(reversed));

output:

[pmurT, dlanoD, olleh]
-1

you can use the following code

public static List<String> reverseList(List<String> list) {
        List<String> reversed = new ArrayList<>(list);
        Collections.reverse(reversed);
        return reversed.stream().collect(Collectors.toList());
    }
-1

This code can be used for String array reverse implementation. There are many ways of reversing a string. One is using StringBuilder I have used.

public class Main
{
 static String[] arr={"hello", "Donald", "Trump"};
  public static void main (String[]args)
 {
String[] strA=  reverseTheString(arr);

for(int i=0;i<strA.length;i++){
    System.out.println(""+strA[i]);
}
}

// This is actual method for reverse an array elements.

public static String[] reverseTheString(String[] arr){
 String[] reverseArray=new String[arr.length];
int j=arr.length-1;

for(int i=0;i<arr.length;i++)
  {
      StringBuilder sb=new StringBuilder(arr[i]);
    reverseArray[j]=sb.reverse().toString();
    j--;
 }
return reverseArray;
 }
}
garima garg
  • 308
  • 1
  • 8
  • Not sure why its downvoted. It's giving the required result and user dint write any specific algo/logic to use. – garima garg Aug 19 '20 at 13:34