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 6 of 6

Thread: Having problems understanding String Methods

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Having problems understanding String Methods

    I'm not sure if I'm using the replaceAll method correctly. Based on the example below, if I'm using Public String replaceAll method, do I need to use " around each string inside the parenthesis? Am I using the method correctly?

    Example/:
    exampleString = "Hello, Jenn. How are you, Jenn?";
    exampleString = exampleString.replaceAll("Jenn", "Kim");

    I want to change the string to substitute Kim for Jenn. I'm confused because my textbook is telling me that string are immutable, or can't be changed. Do I have to assign a new name to my string entirely or will my example above work? If it works, how can a string be immutable but allow me to alter it?


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Having problems understanding String Methods

    i couldn't figure out how to get it to work like you said but you could do it like this...I'm also not sure how you are getting the name (from a console input maybe?)


    String s; 
    		s = "Jenn";
    		System.out.printf("Hello, %s. How are you, %s?\n", s, s);
     
    		s = "Kim";
    		System.out.printf("Hello, %s. How are you, %s?", s, s);

    well i was playing around with it and this works...refined it lol

    		String s = "Hello Jenn";
    		System.out.println(s);
    		System.out.println(s.replaceAll("Jenn", "Kim"));

    i learned something new from this and hopefully you did too

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having problems understanding String Methods

    String s; 
    		s = "Jenn";
    		System.out.printf("Hello, %s. How are you, %s?\n", s, s);
     
    		s = "Kim";
    		System.out.printf("Hello, %s. How are you, %s?", s, s);

    I can't use the example above as the string that I'm working with is user inputed, so I don't know the values that I'm trying to replace until they tell me.

    		String s = "Hello Jenn";
    		System.out.println(s);
    		System.out.println(s.replaceAll("Jenn", "Kim"));

    I'm still new at this, so bear with me. In this example, you are printing the original string and then printing the string with the replacement of Kim for Jenn, correct? Can I do this without the print by just assigning the value to the string like so:
    s = s.replaceAll("Jenn","Kim");

    Also, any idea why my book is telling me that I can't reassign a value to a string? I'm not sure that I understand that.

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Having problems understanding String Methods

    thats correct i printed both just to show that it changed the name..ah yes the s = s.replaceAll("Jenn", "Kim");
    works outside the println you are correct

    also if you are getting the names from user input you should probably store the names as variables and use the variables instead of "Jenn" and "Kim" unless one or both of those never changes

    sorry forgot about the second part of your question

    im not sure why its saying you cant reassign a value to it...maybe its not good practice to do that?

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having problems understanding String Methods

    If I'm using a named variable for the string target and string replacement, I do not need the quotes, correct?

  6. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Having problems understanding String Methods

    nope you dont need the quotes

    say you have 2 variables name1 and name2
    example: s = s.replaceAll(name1, name2);

Similar Threads

  1. Problems with understanding inheritance/method overiding
    By Jackbruns28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 26th, 2013, 08:50 PM
  2. Need help understanding recrursive and static methods
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2012, 08:23 PM
  3. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 01:08 AM
  4. Having problems understanding what is going on in this linked stack.
    By vysero in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2012, 12:08 AM
  5. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM