Reputation: 90
Ok, so basically, say I have the following code, so I split up the time. But I want to take the individual data from the split and store them into the variables. So like, I split the data, and so far I just print it out split up, but what I want to do is like print out
Hour 15 Minute 42 Second 13
If that makes any sense. Is this possible to do? Thanks.
public class task2 {
public static void main(String[] args){
String hour;
String minute;
String second;
String dateTime = "15:42:13";
String array [] = dateTime.split(" ");
for (int i = 0; i < array.length; i++){
System.out.println(array[i]);
}
//Set individual data into variables and them print out
}
}
Upvotes: 1
Views: 1471
Reputation: 13844
public class task2 {
public static void main(String[] args){
String hour;
String minute;
String second;
String dateTime = "15:42:13";
String array [] = dateTime.split(":");
hour=array[0];
minute=array[1];second=array[2];
System.out.print("Hour "+hour);System.out.print(" "); System.out.print("Minute "+minute);System.out.print(" ");
System.out.print("Second "+second);
//Set individual data into variables and them print out
}
}
output Hour 15 Minute 42 Second 13
Upvotes: 1
Reputation: 1315
Try this
public class task2 {
public static void main(String[] args){
String dateTime = "15:42:13";
String array [] = dateTime.split(":");
//Set individual data into variables and them print out
String hour = array[0];
String minute = array[1];
String second = array[2];
}
}
Upvotes: 0
Reputation: 491
public class task2 {
public static void main(String[] args){
String dateTime = "15:42:13";
String array [] = dateTime.split(":");
String hour = array[0];
String minute = array[1];
String second = array[2];
//...
}
}
Upvotes: 0
Reputation: 121998
Yes it's possible. What you have to do is, split with :
String array [] = dateTime.split(":");
That gives
15
42
13
Then you can assign like, without for loop
String hour = array[0];
String minute = array[1];
String second = array[2];
Upvotes: 1