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

Thread: Using the replace method?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Using the replace method?

    I am trying to write a program that takes the word Mississippi and replaces all the i's with !'s and all s's with $'s. It doesnt allow me to compile the code because it says something about a syntax error on token "!". Here is the code so far:

    public class Replace 
    {
    	public static void main(String[] args)
    	{
    		String word = "Mississippi" ;
    		String word2 = word.replace(i, !) ;
    		String word3 = word2.replace(s, $) ;
     
    		System.out.println(word3);
    	}
    }

    Could anyone help?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using the replace method?

    Have you tried reading the Javadoc? There's an example which might help you figure out why you're getting the compile error.

    edit:

    gah, embarrassing error. I really need to keep my methods straight
    Last edited by helloworld922; September 22nd, 2012 at 04:13 PM.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Using the replace method?

    Quote Originally Posted by helloworld922 View Post
    Have you tried reading the Javadoc... (replace doesn't replace all instances, it only replaces the first instance it finds)...
    Ummm...
    From String (Java Platform SE 6) and from the Java 7 reference whose link you posted:

    String replace(char oldChar, char newChar)
    Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar
    .


    Cheers!

    Z
    Last edited by Zaphod_b; September 22nd, 2012 at 04:21 PM.

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    helloworld922 (September 22nd, 2012)

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using the replace method?

    Quote Originally Posted by Zaphod_b View Post
    Ummm...
    From String (Java Platform SE 6) and from the Java 7 reference whose link you posted:



    Cheers!

    Z
    ::blush:: absolutely right.

  6. #5
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Using the replace method?

    Quote Originally Posted by Zaphod_b View Post
    Ummm...
    From String (Java Platform SE 6) and from the Java 7 reference whose link you posted:



    Cheers!

    Z
    Can't believe I forgot the quotation marks -_- . Thanks guys!

Similar Threads

  1. Problem with String replace and CharSequence
    By Calii in forum Java Theory & Questions
    Replies: 3
    Last Post: October 4th, 2011, 07:32 PM
  2. [SOLVED] Replace String Method? Noobie :/
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 18th, 2011, 10:58 AM
  3. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  4. [SOLVED] Replace Phrase in String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2010, 12:10 PM
  5. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM