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: any idea?StringBuilder

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default any idea?StringBuilder

    Hello everyone,
    how can i replace each letter for example "abcde" to "edcba" with StringBuilder only and without the reverse tool
    this is what i tried:
    StringBuilder str=new StringBuilder("abcde");
    		int indexBegin=0;
    		int indexEnd=4;
     
    		for(int i=0;i<str.length();i++){
    			str.setCharAt(i, str.charAt(indexEnd));
    			indexEnd--;
    		}
     
    		System.out.println(str);
    the output is:edcde with i understand why its wrong ,the last two letters already swap so it didnt take from the original str.
    any thoughts how to improve it?
    thanks:}

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: any idea?StringBuilder

    Well, you almost answered your own question. If you do it like this you will overwrite one half of the buffered string before you can use all characters. The simple solution is to make a local copy of the String, a backup you might say, so that you always have full access to all characters at any point in time.

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: any idea?StringBuilder

    great i understoond and it works well via for loops.
    what isnt it working with my recursion:
    public  static void switchLetters(StringBuilder str,int indexBegin,int indexEnd){
    		StringBuilder k=new StringBuilder(str);
    		if(indexEnd==0){
    			return;
    		}
     
    		str.setCharAt(indexBegin, k.charAt(indexEnd));
     
    		switchLetters(str,++indexBegin,--indexEnd);
     
    		System.out.println(str);
    	}
    in recursion it return only "edcde" what isnt it work here?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: any idea?StringBuilder

    Try to write both "str" and "k" to System.out in every step of the recursion and have a look if you can figure out your problem on your own.
    If you dont see it, reply again and tell us what you have problems with.

Similar Threads

  1. [SOLVED] JOptionPane and StringBuilder
    By javaStooge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 6th, 2014, 10:30 PM
  2. clearing StringBuilder
    By tonynsx in forum Java Theory & Questions
    Replies: 6
    Last Post: October 31st, 2013, 10:28 PM
  3. StringBuilder Vs StringBuffer
    By qazi in forum Java Theory & Questions
    Replies: 0
    Last Post: July 28th, 2013, 08:31 AM
  4. StringBuilder
    By Anoop Shiralige in forum Java Theory & Questions
    Replies: 1
    Last Post: June 23rd, 2012, 07:08 AM
  5. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 09:42 PM