Results 1 to 3 of 3
  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Problem With String Reverse

    Hi,

    I've written this code. I want it to read in a series of words from a user and for it to print the input as one string. I then want it to print the string with the word order reversed. For example,

    Forza Inter

    then print

    Inter Forza

    The problem is it prints

    Forza Inter

    then

    retnI azroF

    I know this is because I am using string reverse but is there another method I could use instead to do what I want? I could use an array, which I know how to use, but the problem with that would be when I set it to x words that would be its limit and I need it to accept unlimited input.

    Java Code:
    // Program to read in a series of strings from a user, using JOptionPane, print the string and print the string with the words reversed
    
    import javax.swing.JOptionPane; //import JOptionPane
    public class SentenceReverse //class declaration
    { //open class
    	public static void main (String[] args)
    		{ //open args
    		int length; //Declare int length
    		String sentence=""; //Declare int sum
    		String word; //Declare string word
    			do //Do
    			{ //Open do
    			word=JOptionPane.showInputDialog(null, "Please enter your message word-by-word: "); //Ask for user input
    			sentence += " ";
    			sentence += word; //Sum is sum plus length
    			sentence += " ";
    			length=word.length(); //Get string length
    			} while (length != 0); //Close do when length = 0
    			String reverse = new StringBuffer(sentence).reverse().toString();
    			JOptionPane.showMessageDialog(null, "The total number of characters in those words is " + sentence + "."); //Print sum message
    			JOptionPane.showMessageDialog(null, "The total number of characters in those words is " + reverse + "."); //Print sum message
    			System.exit(0); //program terminates safely
    		} //close main method
    } //close class
    Thanks, and sorry for such a basic question.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,565
    Rep Power
    23

    Default Re: Problem With String Reverse

    Look at the Collections class. It has a method that will reverse a list

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

Similar Threads

  1. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  2. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 05:39 AM
  3. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 02:31 AM
  4. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 12:29 AM
  5. reverse a string with a while loop...
    By OptimusPrime in forum New To Java
    Replies: 9
    Last Post: 12-28-2009, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •