fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. ResultOfIndexOf result = StringUtil.indexOf("hello, world", 'l');
  10. if (result.isFound())
  11. System.out.println(String.format("index=%d", result.foundIndex()));
  12. }
  13. }
  14.  
  15. class StringUtil {
  16. public static ResultOfIndexOf indexOf(String haystack, char needle) {
  17. for (int i = 0; i < haystack.length(); ++i)
  18. if (haystack.charAt(i) == needle)
  19. return new ResultOfIndexOf(i);
  20. return new ResultOfIndexOf();
  21. }
  22. }
  23.  
  24. class ResultOfIndexOf {
  25. private final boolean isFound;
  26. private final int foundIndex;
  27. ResultOfIndexOf() {
  28. this(false, 0);
  29. }
  30. ResultOfIndexOf(int foundIndex) {
  31. this(true, foundIndex);
  32. }
  33. private ResultOfIndexOf(boolean isFound, int foundIndex) {
  34. this.isFound = isFound;
  35. this.foundIndex = foundIndex;
  36. }
  37. public boolean isFound() {
  38. return isFound;
  39. }
  40. public int foundIndex() {
  41. if (!isFound)
  42. throw new RuntimeException("index not found");
  43. return foundIndex;
  44. }
  45. }
  46.  
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
index=2