#I want to run the same method on a single string, Twice.

1 messages · Page 1 of 1 (latest)

bitter dockBOT
#

This post has been reserved for your question.

Hey @autumn gyro! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

autumn gyro
#

Let me rephrase

#

So I run a loop where it calls the same method, but it is implemented by two different classes

#

The method is called 'maakOp'

#

1 Class that implemented it, is supposed to make the String Capitalized. The Second Class that implemented it, is supposed to replace a certain word in the giving String.

autumn gyro
# autumn gyro

Here you can see, I have an ArrayList of 'processes' where the method needs to be called on.

#

On Line 11 it gives me an correct answer (it replaced 'hij' with 'hij/zij')

timid salmon
#

set resultaat to input and just use that

#

you want the string to be passed through the 2 methods, so the result of the first method needs to be passed to the second method

#

you have resultaat there that's used to recieve the result of the methods, so use that to operate

autumn gyro
#

Second iteration (now it capitalizes the string, but the action of the first method is not executed)

autumn gyro
timid salmon
#

yeah that's because you're using input separately each time, so it basically just resets the result from the previous operation

autumn gyro
#

So completely remove the String resultaat variable?

timid salmon
#

no, it's bad practice to reassign to your parameters, so you should be using resultaat

autumn gyro
#
    public String verwerk (String input){
        String resultaat = input;
        for (OpmaakProces proces : processen){
            resultaat = proces.maakOp(input);
        } return resultaat;
    };
timid salmon
#

(alternate approach to the same solution)
imagine doing it without the loop

String result1 = processen.get(0).maakOp(input);
String result2 = processen.get(1).maakOp(result1);
String resultaat = result2;
```now we want to make those 2 first lines basically the same except the index, so we can put it in the loop
so change them to reuse the same variable:
```java
String resultaat = processen.get(0).maakOp(input);
resultaat = processen.get(1).maakOp(resultaat);
resultaat = resultaat; // now unnecessary
```separate the declaration
```java
String resultaat;
resultaat = processen.get(0).maakOp(input);
resultaat = processen.get(1).maakOp(resultaat);
```make `resultaat` use `input`:
```java
String resultaat = input;
resultaat = processen.get(0).maakOp(resultaat);
resultaat = processen.get(1).maakOp(resultaat);
```now you can put those near identical lines in the loop
autumn gyro
#

That makes a bunch of sense!

#

Took a bit but the quarter fell

#

Appreciate it bud! Thanks a lot @timid salmon

bitter dockBOT