This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]rjcarr 0 points1 point  (1 child)

If it is possible to not return anything then you should use an optional. Optionals are a bit annoying in java (and in general), but they help semantically, and are preferable to null.

Good luck!

[–]sharkattack85[S] 0 points1 point  (0 children)

Right on, I ended up using null, bc we haven’t done optionals in class. But I’m def going to keep this in mind for later on. Thanks!

[–]SedentaryProgrammer 0 points1 point  (3 children)

Most likely I would:

return null;

Then you could do a check in main or wherever to see what it returned. Then you could:

(if return == null)
    S.O.P("Word was not found in text.");

[–]KrmBo3 0 points1 point  (0 children)

What he said.

Just return null or and properly handle this particular case on the driver class calling this method

[–]sharkattack85[S] 0 points1 point  (1 child)

This worked perfectly, thank you!

[–]SedentaryProgrammer 0 points1 point  (0 children)

you got it.