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

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




Reversing the order of a sentence

 
Reply to this topicStart new topic

Reversing the order of a sentence

noITpro
6 Feb, 2008 - 08:38 AM
Post #1

New D.I.C Head
*

Joined: 14 Nov, 2007
Posts: 12



Thanked: 1 times
My Contributions
hi all!

i have searched on this forum and in all my java textbooks available to do this but in vain. all i found was how to do it in VB.net or C++ etc. which didn't help at all.
so can someone please kindly hint me here...

1. a user inputs a sentence
2. needs to be converted to capitals
3. i need to replace vowels with other consonants
4. reverse the sentence

i managed to do up till number 3 but couldn't do number 4!

Front end class:
CODE
import javax.swing.*;

public class RunEn
{
    public static void main(String[] args)
    {
        //var
        String sent;
        
        //in
        sent = JOptionPane.showInputDialog(null,"Please input sentence: ");
        
        //pro
        En obj = new En(sent);
        
        //out
        JOptionPane.showMessageDialog(null,"Encrypted version: "+obj.getFinal());
    }    
}


Back end class:
CODE
public class En
{
    private String sent, caps, vows, vows1, vows2, vows3, vows4;
    public En(String sentT)
    {
        sent = sentT;
        caps = sent.toUpperCase();
        vows = caps.replace('A','Z');
        vows1 = vows.replace('E','Z');
        vows2 = vows1.replace('I','Z');  
        vows3 = vows2.replace('O','Z');
        vows4 = vows3.replace('U','Z');
    }    

    public void setOrd()
    {
        //this is where the reversing thing is suppose to be but i dunno how to do it...???
    }
    
    public String getFinal()
    {
        return vows4;
    }
}


thanks!
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 08:47 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
What I would do is parse the sentence on the blank space, then read each word into an array element. Once you have all of the words in an array, just do a for loop until you've switched everything. Then it should be backwards.
User is offlineProfile CardPM
+Quote Post

noITpro
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 09:05 AM
Post #3

New D.I.C Head
*

Joined: 14 Nov, 2007
Posts: 12



Thanked: 1 times
My Contributions
QUOTE(no2pencil @ 6 Feb, 2008 - 09:47 AM) *

What I would do is parse the sentence on the blank space, then read each word into an array element. Once you have all of the words in an array, just do a for loop until you've switched everything. Then it should be backwards.


i haven't done arrays yet... so that might be a problem.
but it suggests that i should use:
-switch case
-length()
-toUpperCase()
-charAt()

i don't quite understand how that would help???
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 09:06 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You would use toUpperCase() for #2. (2. needs to be converted to capitals)
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 09:22 AM
Post #5

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Reverse or of the words or the whole sentence?
like:
QUOTE

CODE

hello im dillon = dillon im hello

or
CODE
nollid mi olleh

To reverse order you need to convert your sentence to a char[]
then use a for loop:
CODE

string sentenceBackwords = "";
for(int counter = yourCharArray.length - 1; counter > 0; counter--)
//-1 because an array's length is 1 more than its last index
{
        sentenceBackwords += yourCharArray[counter]'
}

At least I think that does it.. im don't have admin rights at school so I can't check for you sad.gif

This post has been edited by DillonSalsman: 6 Feb, 2008 - 09:24 AM
User is offlineProfile CardPM
+Quote Post

noITpro
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 09:41 AM
Post #6

New D.I.C Head
*

Joined: 14 Nov, 2007
Posts: 12



Thanked: 1 times
My Contributions
QUOTE(DillonSalsman @ 6 Feb, 2008 - 10:22 AM) *

Reverse or of the words or the whole sentence?
like:
QUOTE

CODE

hello im dillon = dillon im hello

or
CODE
nollid mi olleh

To reverse order you need to convert your sentence to a char[]
then use a for loop:
CODE

string sentenceBackwords = "";
for(int counter = yourCharArray.length - 1; counter > 0; counter--)
//-1 because an array's length is 1 more than its last index
{
        sentenceBackwords += yourCharArray[counter]'
}

At least I think that does it.. im don't have admin rights at school so I can't check for you sad.gif


i think its just reverse the words.
is an array my only solution?
because i know nothing about them. i heard they were quite difficult!
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 11:21 AM
Post #7

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
First of all, yes your going to have to use an array.
There is a tutorial for them here:
http://www.dreamincode.net/forums/showtopic27806.htm

Okay so what you are going to need to do then is:
CODE

String sentenceBackwards = "";
for(int counter = args.length - 1; counter >= 0; counter--)
{
     sentenceBackwards += args[counter] + " ";
}
System.out.println(sentenceBackwards);


I think.. like I said, I'm at school right now but it looks legit to me biggrin.gif
Also, I don't know if you will be marked down for this but there will be a space after the last word.. if it does you just change:
CODE

System.out.println(sentenceBackwards);
//to
System.out.println(sentenceBackwards + "\b");

I think thats what the backspace code is...
Anyways hope it helps wink2.gif

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 01:13 PM
Post #8

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
the backwards command is probably not the best way to go, although it will work, try using:
System.out.println(sentenceBackwards.trim());
instead, it simply removes whitespace from the beginning and end.
User is offlineProfile CardPM
+Quote Post

potator
RE: Reversing The Order Of A Sentence
6 Feb, 2008 - 06:25 PM
Post #9

D.I.C Head
Group Icon

Joined: 2 Dec, 2007
Posts: 78



Thanked: 1 times
Dream Kudos: 175
My Contributions
Arrays aren't hard at all and they are really the building blocks of more complex programming. There is a very simple way to reverse word order using the built in methods.
CODE

String sampleIn = "THIS IS A SAMPLE";
String sampleOut = "";
String[] sampleArray = sample.split(" ");
for(int x = sampleArray.length - 1; x > 0; x--)
    sampleOut += sampleArray[x] + " ";
sampleOut += sampleArray[0];

The above code uses a method from the String class called split. It 'splits' the specified string at every occurrence of the specified parameter. The loop then adds all the words to the output in reverse order.
User is offlineProfile CardPM
+Quote Post

noITpro
RE: Reversing The Order Of A Sentence
7 Feb, 2008 - 08:46 AM
Post #10

New D.I.C Head
*

Joined: 14 Nov, 2007
Posts: 12



Thanked: 1 times
My Contributions
thanks everyone for you help!
i have sorted the problem out and i didn't need to use arrays.
i don't have the code with me at the moment but if you would like me to put it on. i would gladly do so.
just shout!
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Reversing The Order Of A Sentence
7 Feb, 2008 - 08:58 AM
Post #11

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
QUOTE(potator @ 6 Feb, 2008 - 07:25 PM) *

Arrays aren't hard at all and they are really the building blocks of more complex programming. There is a very simple way to reverse word order using the built in methods.
CODE

String sampleIn = "THIS IS A SAMPLE";
String sampleOut = "";
String[] sampleArray = sample.split(" ");
for(int x = sampleArray.length - 1; x > 0; x--)
    sampleOut += sampleArray[x] + " ";
sampleOut += sampleArray[0];

The above code uses a method from the String class called split. It 'splits' the specified string at every occurrence of the specified parameter. The loop then adds all the words to the output in reverse order.


Except for he wants the sentence that the user inputs on launch tongue.gif
This is much more useful if you have a JTextBox, the user types as much as they want and then you:
CODE

String sentenceIn = myInTextBox.getText();
String sentenceOut = "";
String[] sentenceArray = sentenceIn.split(" ");
for(int x = sentenceArray.length - 1; x > 0; x--)
{
       sampleOut += sentenceArray[x] + " ";
}
sentenceOut += sentenceArray[0];
myOutTextBox.setText(sentenceOut);

At least I think thats how it would go... NetBeans ruined my Swing coding skills tongue.gif I don't need them unless I am doing something without it.

This post has been edited by DillonSalsman: 7 Feb, 2008 - 09:07 AM
User is offlineProfile CardPM
+Quote Post

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

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