replace without using .replaceAll

I am a student and here are the instructions for the assignment:

This program prompts for a single line of text, which you can assume contains the word "hate" (lower case only). The program repeats the line, except replacing the word "hate" with "love." For example:

Enter a line of text: I hate broccoli.

I love broccoli.

You are not allowed to use the Java methods replaceAll or replaceFirst in writing your solution.

Please help me get started.

[486 byte] By [skotwarra] at [2007-11-26 17:28:52]
# 1

class Unhaterator { //just love here

... some code ...

... maybe some more code ...

... No one is going to do your homework for you! ...

}

Keith.

corlettka at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...
# 2
Take a look at the methods substring and indexOf in the String class.Kaj
kajbja at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...
# 3

Suppose your line is input..u want to replace a keyword (for example oldVal) with a new key (for example newVal)..

pass those argument to this method...that will do for u..and will return the news line

public String replaceString(String input,String oldVal,String newVal){

int i = input.indexOf(oldVal);

String buffer1 = input.substring(0,i);

String buffer2 = input.substring(i+oldVal.length(),input.length());

String output = buffer1+newVal+buffer2;

retrun output;

}

cheers......gantait

debduttaa at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...
# 4

> Suppose your line is input..u want to replace a

> keyword (for example oldVal) with a new key (for

> example newVal)..

> pass those argument to this method...that will do for

> u

It's good that your method throws an exception if:

* the string can't be found

* oldVal or input is null

...and it doesn't work if the value is found more than once.

Despite of that. What do you think the OP learns if you do his homework?

Kaj

kajbja at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...
# 5

thnx for mentioning the point..actually this is not a professional code just for example...the null checking he have to do for all three arguments and that's true will not work for every value as it is not in loop..

but the point that u mention is really good...our purpose is to help others by sharing the problems and solutions and not with the all codes that will stop his/her thinking power

debduttaa at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...
# 6
someone posted this exact same question a while back.
mkoryaka at 2007-7-8 23:56:48 > top of Java-index,Java Essentials,New To Java...