Is there an easy way to access the array elements after the parsing of String to Integer which would be different to my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there an easy way to access the array elements after the parsing of String to Integer which would be different to my code?

import java.util.regex.Pattern; import java.text.ParseException; public class Program { public static void main(String[] args) { String initial_match_time_to_split = "20:10";         String[] output = initial_match_time_to_split.split(":");         int hours = Integer.parseInt(output[0]);         int minutes = Integer.parseInt(output[1]);                 System.out.println(hours);         System.out.println(minutes); } }        

15th Sep 2017, 9:19 PM
Sibusiso Mbambo
6 Answers
+ 13
Take a look at the reference of the DateTimeFormatter class... or use LocalTime.parse method.
15th Sep 2017, 9:28 PM
Tashi N
Tashi N - avatar
+ 13
Just forgot... After parsing to LocalTime you can use get methods for hours and minutes. No need to use an array ;)
15th Sep 2017, 9:31 PM
Tashi N
Tashi N - avatar
+ 11
That's what's the formatter is for... you define your own format with it. That makes it possible to simply parse it after that.
15th Sep 2017, 9:33 PM
Tashi N
Tashi N - avatar
+ 2
You could use a for in loop, but idk if you'd consider that easier. for(int t: output) { System.out.println(t); }
15th Sep 2017, 9:30 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
thank you for your answers but the code is not related to time, I can use - separater of numbers instead of :
15th Sep 2017, 9:32 PM
Sibusiso Mbambo
+ 1
thank you
15th Sep 2017, 9:35 PM
Sibusiso Mbambo