• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginning Java class quesiton  RSS feed

 
Matt King
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm taking a beginner java class and I have what I've already seen posted in this forum, the question
Write a program that starts with a line of text and then outputs that line of text with the first occurrence of "hate" changed to "love". For example, a possible sample output might be:
The line of text to be changed is: I hate you.
I ha ve rephrased that line to read: I love you.
You can assume that the word "hate" occurs in the input. If the word "hate" occurs more than once in the line, your program will replace only the first occurrence of "hate". Since we will not discuss input until Chapter 2, use a defined constant for the string to be changed. To make your program work for another string, you should only need to change the definition of this defined constant.
DO NOT USE THE replace(), replaceAll(), nor replaceFirst() functions. Please look into using indexOf() and substring() functions.


I have read and I've found an answer and I made my code work, but it was through trial and error and I really don't quite understand what I did.
Could someone have a look and explain why my



Works, mostly the substring(0,pos) + good + sentence.substring?

I'm not 100% sure as to the why behind it, I get the taking the sentence and starting at pos0 and changing the word, is that it or am I missing something else?

Thank you all!
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write the String down on a piece of paper and under each letter write the digit for its position"

Look where "hate" starts (found by indexOf)- location 2 (value in pos)  add to it the length of "hate" (4) gives 6 which is the starting value for the second substring.


For testing, print out all the values returned by the calls to substring in your code so you can see what the method substring returns.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gives you the index of the beginning of the word "hate" in the string named "sentence".
will be the position of the first character after the word "hate"
is a new string that is a substring of sentence, beginning at position 0 and ending just before index 'pos'.
So that substring is "I ".
Then the "+ good" creates another new String by appending the contents of 'good' to that substring, resulting in the new String "I love".
creates a new String beginning at position (pos + bad.length) and ending at the end of the original String. That String is therefore " you". That String is then appended to the second substring that was created, producing a new String "I love you".

All of the intermediate Strings are then available fop garbage collection, since there is now no reference to any of them.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is much more to say, but well done reading things and getting that far
And . . . welcome to the Ranch
 
Matt King
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote: gives you the index of the beginning of the word "hate" in the string named "sentence".
will be the position of the first character after the word "hate"
is a new string that is a substring of sentence, beginning at position 0 and ending just before index 'pos'.
So that substring is "I ".
Then the "+ good" creates another new String by appending the contents of 'good' to that substring, resulting in the new String "I love".
creates a new String beginning at position (pos + bad.length) and ending at the end of the original String. That String is therefore " you". That String is then appended to the second substring that was created, producing a new String "I love you".

All of the intermediate Strings are then available fop garbage collection, since there is now no reference to any of them.


Thank you so very much for your description and explaining it in such a well thoughtout way.

I kinda had an idea about the string + substring = new string but I wasn't 100% sure.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it is to put the entire String into a StringBuilder object. Go through its methods and find whether you can find anything for deleting or inserting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!