• Post Reply Bookmark Topic Watch Topic
  • New Topic

using .split() method to reverse a string.  RSS feed

 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. This is my assignment:

In this exercise, create a program that asks a user for a phrase, then returns the phrase with the words in reverse order. Use the String class's .split() method for this.

Example input
The rain in Spain falls mainly on the plain

Example output
plain the on mainly falls Spain in rain The


While I understand the assignment, nowhere in the text is it covered how to reverse the order of the words in the string. I understand using the .split method, but not for this excercise.
Here is my code so far.



As you can see, I have been googling this method and have come up nearly empty. My text does not cover the .reverse() method. The only thing it covers with .split() is how to split a string. I also tried StringBuilder with no success.
Thanks.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Carter wrote:My text does not cover the .reverse() method.

That would be because it doesn't exist.

Strings are immutable in Java - you can't change them.
String.split returns an array containing each of the words in the array. You just need to loop through that array in reverse order and build a new String with the tokens. There's one other thing you need to do but that should become obvious once you've build the new String.
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! That helped when I realized that I needed to use StringTokenizer.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did I write that made you think you needed to use StringTokenizer ? As it says in the javadoc
StringTokenizer is a legacy class ... its use is discouraged in new code. ... use the split method of String or the java.util.regex package instead.


You also shouldn't concatenate Strings using + in a loop. Performance is notably better using a StringBuilder.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you didn't need to use a StringTokenizer (though you can). The split() method does the job just as well, and you can use it in a very similar way. Since your assignment specifically tells you to use split(), not using it might not get full credit (depending on the marking policy).
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeez, you are right.
Well, we use codeEngine to compile and submit codes, and it passed.
But yes I should have included .split()
I need to try researching that later when I am done with my other assignments.
Thanks!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might help if you StopCoding(←click) for a few minutes and try to figure this out with paper and pencil. Use the example you were given. Then, given the result of split(), try to explain how to get the desired output from there. Try to do the simplest and most straightforward thing you could possibly think of to get that desired output. Sometimes we make it harder on ourselves than it has to be by dreaming up complicated solutions when a simple one would do just fine.

Do you know how to use any other kind of loop, like the for-loop?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!