0

I need to add a space after splitting a String with a " " delimiter. The reason I need to do this is because a space has not been added at the start of the next String as shown in this example:

"There was nothing so VERY remarkable in that; nor did Alice" + "think it so VERY much out of the way to hear the Rabbit say to" 

My code below only splits the string (and reverses it), but the concatenations produce the following result (Notice where "Alicethink" is joined together):

erehT saw gnihton os YREV elbakramer ni ;taht ron did knihtecilA ti os YREV hcum tuo fo eht yaw ot raeh eht tibbaR yas 

Code:

String[] text = PROBLEM5PARA.split(" "); String reverseString = ""; for (int i = 0; i < text.length; i++) { String word = text[i]; String reverseWord = ""; for (int j = word.length()-1; j >= 0; j--) { reverseWord += word.charAt(j); } reverseString += reverseWord + " "; } System.out.println(reverseString); 

How can I fix it?

EDIT: Here is the whole String:

private static final String PROBLEM5PARA = " There was nothing so VERY remarkable in that; nor did Alice" + "think it so VERY much out of the way to hear the Rabbit say to" + "itself, `Oh dear! Oh dear! I shall be late!' (when she thought" + "it over afterwards, it occurred to her that she ought to have" + "wondered at this, but at the time it all seemed quite natural);" + "but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-" + "POCKET, and looked at it, and then hurried on, Alice started to" + "her feet, for it flashed across her mind that she had never" + "before seen a rabbit with either a waistcoat-pocket, or a watch to" + "take out of it, and burning with curiosity, she ran across the" + "field after it, and fortunately was just in time to see it pop" + "down a large rabbit-hole under the hedge."; 
1
  • How could you tell which word should be seperated after split ? Commented Oct 9, 2016 at 2:26

2 Answers 2

1

If the original string is

"There was nothing so VERY remarkable in that; nor did Alice" + "think it so VERY much out of the way to hear the Rabbit say to" 

that is seen by your code as

"There was nothing so VERY remarkable in that; nor did Alicethink it so VERY much out of the way to hear the Rabbit say to" 

After that concatenation (which happens in compilation, I believe), Alicethink is a single word, and there is no way to reverse Alice and think individually.

If this is a homework assignment, I think your code is currently doing what it's supposed to. If there is doubt, ask your instructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it is a homework assignment and the task is complete I am asking just out of interest.
0

Your original string is also joined Alice" + "think, you need update it to Alice " +"think or Alice" + " think

2 Comments

I'd like to know if there is a way to do this after the String has been set.
@JamesB no, there isn't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.