-1

For this classwork(as you can see i didn't finish it cause i have no clue how to start this) I am suppose to make a program that when the user enters his/her string, plus the character that replaces all characters with, it changes the characters of original string to a new one. For example, I would enter laptop, and i want to replace it with the letter I, so the new string would become I.

tobereplaced, a character that you want your string to be replaced replacedwith, is when the character from tobereplaced replaces the orginal string the user input. If its empty,null return original string, If it has a string and a character to replace then replace it.

/**
 * Replaces all instances of the character toBeReplaced 
 * with replacedWith in the String str. Remember str.charAt(int i) 
 * gives you the character at a location.
 * @param str
 * @param tobeReplaced
 * @param replacedWith
 * @return
 */
public static String replaceChar(String str, char tobeReplaced, char replacedWith)
{
    return str;
}
  • Based on the documentation comments, I would assume a simple call to `replaceAll` solves the problem, but your description of the assignment appears to differ. What is the output of `System.out.println(replaceChar("laptop", 'l', 'j'));` supposed to be? – Brian S Dec 04 '13 at 22:29
  • What is your question? – Code-Apprentice Dec 04 '13 at 22:31
  • Replace every thing in the string with the letter the user wants to replace it with, so like "laptop",L,LLLLLL, this is what im guessing based off the comment from my professor – user2489931 Dec 04 '13 at 22:57

3 Answers3

1

Ok I suppose you are not allowed to use replaceAll() as that would make this task trivial ;-)

Now what you want to do is take a look at the String javadoc and figure out how you could replace a character using the method substring(int beginIndex, int endIndex).

And then you think of a way how you can combine that in a for loop with charAt(int index) which gives you the character at the specified index.

Actually you could also take a look at split() as that could be also used to replace a character:

String s = "halalo";
String [] split = s.split("a");  
// split now contains: split[0] = "h", split[1] = "l", split[2] = "lo"
Blub
  • 3,762
  • 1
  • 13
  • 24
  • +1 for the `split` idea. Though, I guess, the prof will suspect it is from SO, when he sees it. – Ingo Dec 04 '13 at 23:07
0

The arguments of the method are little confusing. What is the difference between 'tobeReplaced' and 'replacedWith'?

If i understand the problem correctly, the method should accept two arguments:

  1. String input by the user
  2. Character input by the user that replaces the String.

I did not see any conditions being mentioned (like to replace only if string is not empty or only if string contains some characters etc). When there are no conditions, the method can simply return the character input received.

correct me, if my understanding is not right. Thanks.

Siva Atta
  • 81
  • 7
  • tobereplaced, a character that you want your string to be replaced replacedwith, is when the character from tobereplaced replaces the orginal string the user input. If its empty,null return original string, If it has a string and a character to replace then replace it. Sorry bout that – user2489931 Dec 04 '13 at 22:28
  • `replaceChar(s, a, b)` means "replace a with b in s". – Ingo Dec 04 '13 at 22:33
  • an example can probably help. if the arguments to the method are "laptop", 'l', 'j', what is the expected output? – Siva Atta Dec 04 '13 at 22:37
  • @Ingo, That's what the doc comments imply, but the OP's description of the problem differs. – Brian S Dec 04 '13 at 22:54
  • @BrianS True, but the doc comment is clear, the OP's description is so confused as to being almost nonsensical, hence I'd bet on the doc comment. The more so as he himself says in a comment that he is "guessing", and this is also his root problem, as far as I see it. – Ingo Dec 04 '13 at 23:08
0

This is an easy task, because only 3 cases can occur:

replaceChars(s, toBeReplaced, replacement)
  1. s is the empty string. Result is the empty string.
  2. s starts with the character that is to be replaced. Result is replacement + replaceChars(rest, toBeReplaced, replacement) where rest is s without the first character.
  3. s does not start with the character that is to be replaced. Result is first + replaceChars(rest, toBeReplaced, replacement) where firstis the first character of s and rest the rest of s, as before.
Ingo
  • 36,037
  • 5
  • 53
  • 100
  • okay i see where you are going with this, but I am so frustrated with loops, cause I dont know how to insert this logic into a loop. – user2489931 Dec 04 '13 at 22:47
  • 1
    @user2489931, Ingo's recommendation involves recursion, not loops. (Although, as it's [tail call recursion](http://en.wikipedia.org/wiki/Tail_call), it wouldn't be terribly difficult to turn into a loop.) – Brian S Dec 04 '13 at 22:52
  • 0_0, so any tips on how to make a LOOP for this program. – user2489931 Dec 04 '13 at 22:53
  • @user2489931 Begging for copy-pasteable code? The only tip I have is: go, learn the language. – Ingo Dec 04 '13 at 22:57
  • @BrianS It's not exactly tail recursive, as it stands, but of course one could (and should) make it tail recursive by introducing a StringBuilder where the know part of the result is written. The transformation of a tail recursive function to a while loop should be a no brainer, then. – Ingo Dec 04 '13 at 22:59
  • Its not so easy for a new person at java programming, going at a fast pace with tests every otheclass, textnotes every day, and project/hw every class. – user2489931 Dec 04 '13 at 23:00
  • @user2489931 You can change profession if it is too hard for you, and become a janitor, maybe. It's not a shame to do useful work, you know. – Ingo Dec 04 '13 at 23:01
  • @ingo I will never give up on java.What do you suggest for a beginner like me to do in order to get better?I keep getting frustrated with silly programs like these, I spend hours on it, I look through all my resources,notes. In your mind you think that this isnt for me, but I will never give up and try my best, because this is all i want to do for my future. So do you have any tips? – user2489931 Dec 04 '13 at 23:43
  • @user2489931 Learning to program takes time and effort, but then at one point suddenly it will make sense. Don't worry, just keep going, keep practicing and don't give up. Stay positive :-) – Blub Dec 04 '13 at 23:48
  • @EdgarBoda, The text book we had in high school ("Exposure Java") had this equation as the core concept for the teaching methods presented: _Bewilderment + Exposure = Obvious_. It's so true, for so many facents of life. – Brian S Dec 05 '13 at 14:52