fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.* ;
  8. import java.time.format.* ;
  9.  
  10. import java.math.BigDecimal;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17.  
  18. String input = "01/02/2017,546.12,24.2,";
  19. String[] parts = input.split( "," );
  20.  
  21. // Parse the first element, a date-only value.
  22. DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
  23. LocalDate localDate = null;
  24. String inputDate = parts[ 0 ] ;
  25. try
  26. {
  27. localDate = LocalDate.parse( inputDate , f );
  28. } catch ( DateTimeException e )
  29. {
  30. System.out.println( "ERROR - invalid input for LocalDate: " + parts[ 0 ] );
  31. }
  32.  
  33. // Loop the numbers
  34. List < BigDecimal > numbers = new ArrayList <>( parts.length );
  35. for ( int i = 1 ; i < parts.length ; i++ )
  36. { // Start index at 1, skipping over the first element (the date) at index 0.
  37. String s = parts[ i ];
  38. if ( null == s )
  39. {
  40. continue;
  41. }
  42. if ( s.isEmpty( ) )
  43. {
  44. continue;
  45. }
  46. BigDecimal bigDecimal = new BigDecimal( parts[ i ] );
  47. numbers.add( bigDecimal );
  48. }
  49.  
  50. // Goals: (1) Get the year of the date. (2) Get the last number.
  51. Year year = Year.from( localDate ); // Where possible, use an object rather than a mere integer to represent the year.
  52. int y = localDate.getYear( );
  53. BigDecimal lastNumber = numbers.get( numbers.size( ) - 1 ); // Fetch last element from the List.
  54.  
  55. System.out.println("input: " + input );
  56. System.out.println("year.toString(): " + year );
  57. System.out.println("lastNumber.toString(): " + lastNumber );
  58.  
  59.  
  60. }
  61. }
Success #stdin #stdout 0.14s 4386816KB
stdin
Standard input is empty
stdout
input: 01/02/2017,546.12,24.2,
year.toString(): 2017
lastNumber.toString(): 24.2