Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help on ideas to approach this String problem..

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Need help on ideas to approach this String problem..

    I am not very comfortable with Strings in Java. I have a pretty knowledgeable background in C, and I learned that it is not the way to approach problems in Java with a "C" mindset.

    The problem I am facing is, taking a string that contains a sentence and reversing the words. Example would be, "Hi I am Bob" and changing it to "Bob am I Hi". Then returning the String.

    My initial thoughts were to change the string into a character array and then manually doing the work with loops and tedious comparison statements. I quickly realized that there must be a better way but I am not very familiar with strings in Java to know what a more sufficient way would be. Any thoughts/Suggestions would be greatly appreciated.
    Thank you.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help on ideas to approach this String problem..

    Yes, your C mindset is handicapping you. The String class has many methods that will help you do what you've described. Refer to the String documentation of API. If you're not familiar with finding it, simply search 'java string doc', and the top two or 3 results should be what you're looking for.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Need help on ideas to approach this String problem..

    	public static void main(String[] args) {
     
    		String a = "Hi I am Bob";
    		String[] words = a.split(" ");
    		String newOne= "";
     
    		for(int i=words.length-1;i>=0;i--)
    		{
    			newOne+=words[i];
    			newOne+=" ";
    		}
     
    		System.out.println(newOne);
    	}
     
    }

    This is what I came up with. Do you agree that Split is a well used method for this exercise? I also thought that substring might be a good method to use but have not done it that way yet.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help on ideas to approach this String problem..

    Split would be fine for this. Although if you have a period at the end, the period would move with the last word. Not a big problem, though.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Ideas for sloving this problem.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 11th, 2012, 05:18 PM
  2. Best way to approach this problem
    By Myshkin in forum Collections and Generics
    Replies: 1
    Last Post: September 30th, 2012, 09:19 AM
  3. Java Speech API and web.. problem getting ideas
    By yugeshshrestha in forum Java Theory & Questions
    Replies: 0
    Last Post: July 7th, 2012, 02:27 AM