I wish to turn a String looking like this:
String string = "5d3h6m";
Or even like this:
String string = " 44h 22d";
In any order, and with any number of spaces into an int symbolising the time in minutes.
(D=days H=Hours M=Minutes)
The problems I have had:
- Finding the characters in any order
- Getting any number of digits that appear before the letter
The following code is me trying to find a solution to the two problems (I am aware it is terrible code, that is why I am here, and no, I do not wish to be spoonfed):
if(timestring.contains(":")){
String[] timestringarray = timestring.split(":");
int timeinminutes = 0;
int timestringarraylength = timestringarray.length;
while(timestringarraylength>0){
if(timestringarray[timestringarraylength].matches("d{1}|D{1}")){
timestring.replace("d{1}|D{1}","");
long days = TimeUnit.MINUTES.convert(Integer.valueOf(timestring), TimeUnit.DAYS);
int daysint = (int) (long) days;
timeinminutes+=daysint;
}
if(timestringarray[timestringarraylength].matches("h{1}|H{1}")){
timestring.replace("h{1}|H{1}","");
long hours = TimeUnit.MINUTES.convert(Integer.valueOf(timestring), TimeUnit.HOURS);
int hoursint = (int) (long) hours;
timeinminutes+=hoursint;
}
if(timestringarray[timestringarraylength].matches("m{1}|M{1}")){
timestring.replace("m{1}|M{1}","");
long mins = TimeUnit.MINUTES.convert(Integer.valueOf(timestring), TimeUnit.MINUTES);
int minsint = (int) (long) mins;
timeinminutes+=minsint;
}
timestringarraylength--;
}
signConfig.set("Sign.time", Integer.valueOf(timeinminutes));
}
To specify, the String is a time entered by the user and won't change during the processing.
Thanks in advance.