Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
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); } }
6 Antworten
+ 13
Take a look at the reference of the DateTimeFormatter class... or use LocalTime.parse method.
+ 13
Just forgot... After parsing to LocalTime you can use get methods for hours and minutes. No need to use an array ;)
+ 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.
+ 2
You could use a for in loop, but idk if you'd consider that easier.
for(int t: output) {
System.out.println(t);
}
+ 1
thank you for your answers but the code is not related to time, I can use - separater of numbers instead of :
+ 1
thank you