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

Thread: Replace String Method? Noobie :/

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Replace String Method? Noobie :/

    This is a program to change the first word in a user input sentence with the last.

    I am extremely sorry if this is a simple fix but I either get a repeated String or a printout error. I have been at it for days -_-


    int first;
    int last;

    first = Modified.indexOf (' ');
    last = Modified.lastIndexOf(' ')+1;

    String word = Modified.substring(0,first);
    String lastword = Modified.substring(first+1, last+4);
    String Modified2 = Modified.replace(word, lastword);
    System.out.println( Modified2);


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Replace String Method? Noobie :/

    I'm not sure what you're asking for. Post your full program in code tags and copy-and-paste any error output. If your program works, but not correctly, post what you expect the output to be and copy-paste the output of your code.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Replace String Method? Noobie :/

    For debugging you should print out the values of word and lastword so you can see what the code is doing.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Replace String Method? Noobie :/

       import java.util.Scanner;
     
    class Hate
    {
      public static void main (String args[])
      {
        String Original;
        String Modified;
     
        int love;
        int hate;
     
        Scanner in = new Scanner(System.in);
        System.out.println(" Fill in the blanks with something you love and something you hate\n I love ____, but hate ____: ");
        Original = in.nextLine();
     
        love = Original.indexOf("love");
        hate = Original.indexOf("hate");
     
        Modified = Original.substring(0,love) + "hate" + Original.substring(love+4, hate) + "love" + Original.substring(hate+4);
        System.out.println ( "Original line of text: \n" + Original);
        System.out.println ( "The word hate is replaced with love: \n" + Modified);
     
        int first;
        int last;
     
        first = Modified.indexOf (' ');
        last = Modified.lastIndexOf(' ')+1;
     
             String word = Modified.substring(0,first);
           String lastword = Modified.substring(first+1, last+4);
           String Modified2 = Modified.replace(word, lastword);
            System.out.println( Modified2);
     
                              }
    }




    Output:


    Original line of text:
    I love cats but hate dogs
    The word hate is replaced with love:
    I hate cats but love dogs
    hate cats but love dogs hate cats but love dogs


    The bolded is suppose to be modified to replace "I" the first word of the sentence with "dogs" the last word and "dogs" is replaced with the word "I". Just flipping the first and the last word of a sentence.

    Suppose to say : dogs hate cats but love I

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Replace String Method? Noobie :/

    Where did you print out the values of word and lastword for debugging?
    Also print out the values of the first and last variables to see what your code is doing.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Replace String Method? Noobie :/

    Output:

    I love ____, but hate ____:
    [DrJava Input Box]
    Original line of text:
    i love cats but hate dogs
    The word hate is replaced with love:
    i hate cats but love dogs
    First word of sentence: i
    Last word of sentence: dogs
    first value: 1
    last value: 21
    Replacement of the first and last word: dogs hate cats but love dogs

    I was able to replace the first word with the last, but do not know how to replace the last with the first.
    What its suppose to say: "dogs hate cats but love I"

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Replace String Method? Noobie :/

    Nice - it's the 'swap(x, y)' problem all over again! Rather than trying to replace the two with each other, why not try to break the sentence into 3 parts, first-second-third and then reassemble them third-second-first?

    edit: confusing wording? I mean "firstword-middlestuff-lastword" with "lastword-middlestuff-firstword"

  8. The Following User Says Thank You to Sean4u For This Useful Post:

    Usoda (September 18th, 2011)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Replace String Method? Noobie :/

    Once you replace the first word with the last word, there are now two "last" words in the String.
    You may have to do the same trick you use when you are swapping items in an array.

    Something like: first to temp, last to first, temp to last

    EDIT: Too slow this time.

Similar Threads

  1. [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
  2. [SOLVED] Replace Phrase in String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2010, 12:10 PM
  3. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  4. Noobie to Java questions!! :D
    By chansey123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 11:53 AM
  5. string method
    By dstha in forum Object Oriented Programming
    Replies: 3
    Last Post: February 25th, 2010, 08:07 PM

Tags for this Thread