Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public static void main(String[] args) {
  2. ArrayList<String> dirtyList = new ArrayList();
  3. dirtyList.add("Null");
  4. dirtyList.add("1.24");
  5. dirtyList.add("0");
  6. dirtyList.add("0asd");
  7. ArrayList<Double> doublelist = new ArrayList();
  8. for ( String item : dirtyList) {
  9. doublelist.add(parseDouble(item));
  10. }
  11. System.out.println(doublelist);
  12. }
  13.  
  14. // Function to clean data
  15. static Double parseDouble(String x) {
  16. Double a;
  17. try {
  18. a = Double.parseDouble(x);
  19. } catch (Exception ParseException) {
  20. a = (double) 0;
  21. }
  22. return a;
  23. }
  24.  
  25. private static Double parseDouble(String x) {
  26. try {
  27. // use valueOf to return a wrapper object instead
  28. return Double.valueOf(x);
  29. } catch (NumberFormatException e) {
  30. return Double.valueOf(0);
  31. }
  32. }
  33.  
  34. double[] result = Stream.of("Null", "1.24", "0", "0asd")
  35. .mapToDouble(YourClass::parseDouble).toArray();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement