fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.function.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. static Map<Class<?>,Predicate<String>> canParse = new HashMap<>();
  12.  
  13. static {
  14. canParse.put(Integer.TYPE, s -> {try {Integer.parseInt(s); return true;} catch(Exception e) {return false;}});
  15. canParse.put(Long.TYPE, s -> {try {Long.parseLong(s); return true;} catch(Exception e) {return false;}});
  16. };
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. if (canParse.get(Integer.TYPE).test("hello")) {
  20. System.out.println("Can parse hello");
  21. } else {
  22. System.out.println("Cannot parse hello");
  23. }
  24. if (canParse.get(Long.TYPE).test("1234567890123")) {
  25. System.out.println("Can parse 1234567890123");
  26. } else {
  27. System.out.println("Cannot parse 1234567890123");
  28. }
  29. }
  30. }
Success #stdin #stdout 0.09s 711168KB
stdin
Standard input is empty
stdout
Cannot parse hello
Can parse 1234567890123