Welcome to Dream.In.Code
Become a Java Expert!

Join 150,367 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




reversing string order....

 
Reply to this topicStart new topic

reversing string order....

Captian_Amazing
23 Jan, 2008 - 09:24 AM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 10


My Contributions
im not sure exactly how to reverse the order of a string.

Ex. user input = The rain in Spain
i would like it to be = Spain in rain The

any ideas on how to do this?
CODE

import javax.swing.*;

public class reverse_order
{
public static void main(String[] args)
{
    int x=0,
        y;
    String array[] = new String[100];
    String entry;
    StringBuffer message = new
    StringBuffer("The words in reverse order are\n" );

    entry = JOptionPane.showInputDialog(null,"Enter any word\n" + "Enter XXX when you want to stop");
    
    while((entry.equals("XXX")))
        array[x] = entry;
        ++x;
        entry = JOptionPane.showInputDialog(null, "Enter another word\n" + "Enter XXX when you want to stop");
  
    for(y = x; y <= 0; --y)
    {
        message.append(array[y]);
        message.append("\n");
    }
    JOptionPane.showMessageDialog(null, message);
    System.exit(0);
}
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Reversing String Order....
23 Jan, 2008 - 09:34 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Check the StringTokenizer class.
http://java.sun.com/j2se/1.4.2/docs/api/ja...gTokenizer.html[/url]

Use the String array to store the tokens, then just print the array backwards.

This post has been edited by PennyBoki: 23 Jan, 2008 - 09:34 AM
User is offlineProfile CardPM
+Quote Post

Captian_Amazing
RE: Reversing String Order....
23 Jan, 2008 - 09:39 AM
Post #3

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 10


My Contributions
QUOTE(PennyBoki @ 23 Jan, 2008 - 10:34 AM) *

Check the StringTokenizer class.
http://java.sun.com/j2se/1.4.2/docs/api/ja...gTokenizer.html[/url]

Use the String array to store the tokens, then just print the array backwards.


now what would happen if you cant use a tokenizer?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Reversing String Order....
23 Jan, 2008 - 09:57 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well then you can simulate a Tokenizer, loop through the string and check all the chars and create words and store the words into a String array, then print the array backwards.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Reversing String Order....
23 Jan, 2008 - 01:26 PM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
You could do that. Or you could look at the String API and you'll notice a method called split that will chunk it up into an array for you.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Reversing String Order....
24 Jan, 2008 - 09:29 AM
Post #6

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
What - was my answer too terse? I'm assuming the OP could read the API link I provided.

This post has been edited by Programmist: 24 Jan, 2008 - 09:30 AM
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Reversing String Order....
24 Jan, 2008 - 10:00 AM
Post #7

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,592



Thanked: 12 times
Dream Kudos: 325
My Contributions
QUOTE(Programmist @ 24 Jan, 2008 - 10:29 AM) *

What - was my answer too terse? I'm assuming the OP could read the API link I provided.

Sorry, I like coding. smile.gif

I'll edit my post if you want.
User is online!Profile CardPM
+Quote Post

1lacca
RE: Reversing String Order....
24 Jan, 2008 - 10:20 AM
Post #8

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Tom9729, I've hidden your message, because the site's policy clearly states that we don't hand out full solutions before the OP puts some effort into it. If the OP comes back with his implementation, I'll put it back (if I would not notice it, simply pm me or another moderator - it's ID is 41655).
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Reversing String Order....
24 Jan, 2008 - 12:00 PM
Post #9

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,592



Thanked: 12 times
Dream Kudos: 325
My Contributions
QUOTE(1lacca @ 24 Jan, 2008 - 11:20 AM) *

Tom9729, I've hidden your message, because the site's policy clearly states that we don't hand out full solutions before the OP puts some effort into it. If the OP comes back with his implementation, I'll put it back (if I would not notice it, simply pm me or another moderator - it's ID is 41655).

That's fine. I know the site policy (I even offered to remove my post), it just looked to me that the OP already showed some effort, and linking to the API docs saying "use this function" is a bit rough.

Most people learn better by example. smile.gif
User is online!Profile CardPM
+Quote Post

Programmist
RE: Reversing String Order....
24 Jan, 2008 - 04:48 PM
Post #10

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(Tom9729 @ 24 Jan, 2008 - 12:00 PM) *

Sorry, I like coding. smile.gif


There's nothing wrong with liking to code. (It's kind of a requirement to be a successful programmer.) I've often coded a solution to someone's problem, but just not posted it.

QUOTE(Tom9729 @ 24 Jan, 2008 - 02:00 PM) *

That's fine. I know the site policy (I even offered to remove my post), it just looked to me that the OP already showed some effort, and linking to the API docs saying "use this function" is a bit rough.

Most people learn better by example. smile.gif


I have some experience teaching programming and I have found that some people do learn from example. But, in large part, beginners copy examples and modify them without really understanding how things work. As long a you give them code, they will often continue to copy-paste-modify. Only after someone learns how to help themselves by reading the documentation and exploring the API do they really start to understand. I find what works best is to give hints and links to specific documentation and then leave the door open for the student to try for themselves and ask questions if they have any. This approach isn't always welcomed because some people don't like to think for themselves or put in the effort to learn. That's not my problem. I'm happy to help the willing. I learned by reading books, scouring the web (Google), and reading...yes, that's right...reading the fricken manual. smile.gif

On this site we have another problem. We can't tell the people who really want to learn from those just looking for an easy answer, so we have to assume that, if no code has been posted, then there is intent to cheat (even if there is not).
</rant>
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Reversing String Order....
25 Jan, 2008 - 04:21 AM
Post #11

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Exactly, if the OP is an established member here, who we know is really motivated, I would not have any problem with giving him the full solution right in the first reply. Unfortunately with new members we are more likely to stick with the rules.

QUOTE
(I even offered to remove my post),
Appreciated, that's why I wrote that it is not lost, and it'll be put back, because you had work in it, and it was a exactly a contribution that makes the site great!

QUOTE
it just looked to me that the OP already showed some effort, and linking to the API docs saying "use this function" is a bit rough

You are right, but if that first snippet was his work, I think linking the docs was just enough to push him into the right direction. I really agree with Programmist, that it is essential to learn to use the API docs - and just as important to learn to use google. I think that the ideal flow of things would be this: OP asks a question, he is given some documentation, he tries to use it, he posts his answer. If it works fine, then he thanks us smile.gif If it can be improved, or he has problems, then the cavalry can come in. Also, reading the docs is important, because hopefully he'll read the things near the answer, and it gives him a better understanding of the whole picture.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Reversing String Order....
25 Jan, 2008 - 05:10 PM
Post #12

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(1lacca @ 25 Jan, 2008 - 06:21 AM) *

Silence is golden. Duct tape is silver.


Off topic: That's one of the funniest signatures I've seen in a long time. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month