0

(The title may be misleading. I always though the hard part is finding the proper title :D)

Well, sentences are just (long) strings. I want to display these sentences but in a reversed way. Example: "StackOverflow is a community of awesome programmers" would become "programmers awesome of community a is StackOverflow".

So my idea is to have a delimiter, here a blank space. Whenever texts are entered and the space bar pressed, save that word in a list, an ArrayList and then just display them in inverted order in a textView.

So far i can only output texts but without blank spaces (programmersawesomeofcommunityaisStackOverflow) and only using a button. I use the below code to do that:

@Override
        public void onClick(View v) {
            String[] sentence = input.getText().toString().split(" "); //This split() method is the culprit!
            ArrayList<String> wordArray = new ArrayList<>();
            for (String word : sentence) {
                    wordArray.add(word);
            }
            Collections.sort(wordArray);
            StringBuilder invertedSentence = new StringBuilder();
            for (int i = wordArray.size(); i > 0; i--) {
                invertedSentence.append(wordArray.get(i - 1));
            }
            output.setText(invertedSentence.toString());
        }
    });

How can I have a sentence be saved (automatically) in a list as split words when the system detect a white space? And add blank spaces in the output sentences?

Thanks for your time.

esQmo_
  • 1,464
  • 3
  • 18
  • 43
  • Why are you sorting `wordArray`? And the *regex* for whitespace is `\\s`. – Elliott Frisch Aug 15 '18 at 17:09
  • `""` is an _empty string. It doesn't even contain whitespace. I'd assume you either mean `" "` or `"\\s+"` (any whitespace including tabs etc.). Also you might want to add some spaces when assembling the inverted sentence, e.g. via `invertedSentence.append(" ").append(wordArray.get(i - 1));` or yet better use Java 8's streams to invert the array and concatenate the strings using `join(" ")`. – Thomas Aug 15 '18 at 17:10
  • And on a side note: `Collections.sort(wordArray);` would _not_ invert the list but sort it, i.e. your example text would then become `a awesome community is of programmers StackOverflow` (assuming the rest is correct). – Thomas Aug 15 '18 at 17:14
  • @Thomas Thank you. I was trying to find a way to add spaces to the inverted sentence – esQmo_ Aug 15 '18 at 17:14
  • @ElliottFrisch So that I can loop backwards to output the inverted sentence. I was wrong – esQmo_ Aug 15 '18 at 17:18
  • Yes you're right, @Thomas the output is not inverted – esQmo_ Aug 15 '18 at 17:22
  • To reverse the array you could try `Arrays.sort(array, Comparator.reverseOrder())` or for your list just use `Collections.reverseOrder(wordArray)` (and then `wordArray.stream().join(" ")`). – Thomas Aug 15 '18 at 17:26
  • `Collections.reverseOrder(String)` doesn't work. – esQmo_ Aug 15 '18 at 17:28

1 Answers1

1

Many of the comments have good suggestions, but here is one approach you could use:

    String[] sentence = new String("StackOverflow is a community of awesome programmers").split(" ");
    ArrayList<String> wordArray = new ArrayList<>();
    for (String word : sentence) {
       wordArray.add(0, word);
    }

    String backwards = String.join(" ", wordArray);
    System.out.println(backwards);

Output

programmers awesome of community a is StackOverflow
Bill
  • 1,407
  • 10
  • 18