#I want to run the same method on a single string, Twice.
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @autumn gyro! Please use
/closeor theClose Postbutton 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.
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.
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')
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
Second iteration (now it capitalizes the string, but the action of the first method is not executed)
ill try that!
yeah that's because you're using input separately each time, so it basically just resets the result from the previous operation
So completely remove the String resultaat variable?
no, it's bad practice to reassign to your parameters, so you should be using resultaat
public String verwerk (String input){
String resultaat = input;
for (OpmaakProces proces : processen){
resultaat = proces.maakOp(input);
} return resultaat;
};
(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
That makes a bunch of sense!
Took a bit but the quarter fell
Appreciate it bud! Thanks a lot @timid salmon
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.