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.


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

Results 1 to 2 of 2

Thread: char manipulation quick question

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    Default char manipulation quick question

    can i do this and if not what is the appropriate line of code to change the char at position a in sentence to the character at position element in letters

    where sentence and letters are two strings
    and element is an int.

    sentence.charAt(a) = letters.charAt(element)??????????????????????????? ????????

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: char manipulation quick question

    Strings are immutable. Which is a jargon shorthand for saying that an instance of String will not change its characters (think "leopard" and "spots").

    There are two ways you can go. One is to build up a new string based on sentence, letters and element. There are lots of methods in the String class that will help. Once you have that newly constructed string you assign it to sentence.

    The other way is not to use String at all, but prefer StringBuilder. As you can see from its docs a StringBuilder instance is mutable: ie there are methods provided to alter the characters it contains. If you are altering the contents of a sequence of characters a lot (eg Hangman or similar), StringBuilder might be more useful.

Similar Threads

  1. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 01:02 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM
  5. question on list manipulation
    By andrefaelnar in forum Java Theory & Questions
    Replies: 11
    Last Post: August 18th, 2010, 07:30 PM