Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,780 members, 7,817,233 topics. Date: Saturday, 04 May 2024 at 08:28 AM

How do I reverse a paragraph with Java? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How do I reverse a paragraph with Java? (3580 Views)

Learning To Program With Java by the Fulaman / Programing With Java / Developing A Professional Bluetooth Instant Chat Messenger With Java (2) (3) (4)

(1) (Reply) (Go Down)

How do I reverse a paragraph with Java? by 2legit2qwt: 2:46pm On Oct 20, 2011
Java statement to reverse paragraph?
Re: How do I reverse a paragraph with Java? by avosoft: 8:14pm On Oct 20, 2011
This is very easy, this is vb.net, you can look for similar functions in java

'// this line copies/convert the text to character array
Dim characters() As Char = txt1.Text.ToCharArray

'// Reverse the array
Array.Reverse(characters)

'// convert it back to string
txt1.Text = CStr(characters)

enjoy
Re: How do I reverse a paragraph with Java? by SayoMarvel(m): 2:30am On Oct 21, 2011
2legit2qwt:

Goal is to write a program that will reverse each sentence a user enters. Upon reversing the paragraph, it will capitalize the first letter after each period separating the sentences and also delete any period that starts the new paragraph.

If I interpreted your problem statement correctly,
My name is Sayo. I am a student.
should give
.tneduts a ma I .oyaS si eman yM
after reversal, capitalize the first letter after each period separating the sentences, giving
.tneduts a ma I .OyaS si eman yM
(the period after the last "t" in students did not separate sentenses so I'm assuming it will not be capitalized), afterwards, delete the period that starts the new paragraph (which is only the first period after reversal), resulting in
tneduts a ma I .OyaS si eman yM

Am I right?

The code is attached to this reply. Download it and change the extension from jpg to html.

Re: How do I reverse a paragraph with Java? by SayoMarvel(m): 3:03am On Oct 21, 2011
I don't like the way nairaland handles the presentation of "[ccode] [/ccode]". You may want to check out how javaranch.com handles it.
@omo_to_dun how were you able to trick nairaland into rendering your code perfectly? I'm surprised HTML formatting is not working here. Or, am I missing something?
Re: How do I reverse a paragraph with Java? by Nobody: 4:59am On Oct 21, 2011
Re: How do I reverse a paragraph with Java? by SayoMarvel(m): 7:21pm On Oct 21, 2011
Please attach it to your next comment. Thanks
Re: How do I reverse a paragraph with Java? by Nobody: 7:46pm On Oct 21, 2011

Re: How do I reverse a paragraph with Java? by 2legit2qwt: 6:54pm On Oct 23, 2011
Sorry it took me this long to reply, I have been out of town since Friday. A big thank you to everyone who responded and offered to help out, i really appreciate it.
@SayoMarvel Thanks a great deal, I will look at the code in a minute and give you feed back on it, thanks once again
Re: How do I reverse a paragraph with Java? by 2legit2qwt: 7:55pm On Oct 23, 2011
@SayoMarvel So i realized you wrote that in Javascript?? or some other java coding i'm not familiar with. I'm a newbie and this is more like an introductory class so how can I change it to the regular java that i know? Like the one i posted in my first post?
Re: How do I reverse a paragraph with Java? by SayoMarvel(m): 9:47pm On Oct 23, 2011
Its regular java. I just exported it to html (because I thought it will improve the way nairaland renders the code). And those funny things you saw there are javadoc coments (and tags). Ok, here is a plaint version with no effizy.

public class Reverser {

    public static void main(String[] args) {
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        System.out.println("Welcome. Please type your paragraph and hit the "
                + "\"Enter\" key when you are done."wink;

        //fetch your input.
        StringBuilder sb = new StringBuilder(scanner.nextLine().trim());

        //reverse the string/
        sb.reverse();

        //please note that the local variable [i]index [/i]is initially
        //being set to 1 instead of 0. In cases where the paragraph ends with a
        //period, such a period does not separate sentenses so the the character
        //before it should not be capitalized.
        int index = 1;

        //search for all periods.
        while(true) {
            index = sb.indexOf(".", index);
            if (index < 0)
                break;

            //convert the character following the period to its upper case.
            sb.replace(index + 1, index + 2, String.valueOf(sb.charAt(index + 1)).toUpperCase());

            //increment your index so that you won't keep catching the same
            //period over and over.
            index++;
        }

        //delete the period that starts the new paragraph,
        //obviously this is the first period.
        if (sb.charAt(0) == '.')
            sb.deleteCharAt(0);

        System.out.println(sb);
    }
}
Re: How do I reverse a paragraph with Java? by 2legit2qwt: 9:54pm On Oct 23, 2011
OH i thought as much, it was confusing. And you just saved me some painstaking hours trying to figure this out, i have been refreshing NL ever since my last post lol. there ain't no method or class I haven't tried yet, I just don't know how to execute them to fit this program. Thank you so much for that, i really appreciate your help. I should probably have stayed with my Computer Science instead of IS where you basically don't get to learn the classes in detail.
Re: How do I reverse a paragraph with Java? by omotodun2: 5:39am On Oct 24, 2011
@ SayoMarvel

I was instantly banned after attaching the Java code that you requested. Send me an email at omo_to_dun AT yahoo DOT com if you still want it.
Re: How do I reverse a paragraph with Java? by Mobinga: 9:48pm On Oct 24, 2011
At @OP are you talking reversing words or just the entire paragraph?

If its the entire paragraph all you need is just to call the String.reverse method
Re: How do I reverse a paragraph with Java? by Nobody: 10:00pm On Oct 24, 2011
@Mobinga u're hard to reach i have gone far in the xchange game stuff. just wanted some advice from you. my mail is okeowoaderemi@gmail.com. hows life ?

(1) (Reply)

Method To Shuffle Cards In C# / Tip To Get Added To The ALX Room As A Fellow Without Stress / Can Somebody Tell Me The Object-oriented Way Of Becoming Wealthy?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.