dimanche 1 mars 2015

Capitalize specific character from HashMap String



I have a program which converts numbers 0-9 in a String to their text equivalents (e.g: "0" becomes "zero"). However, when the number is at the beginning of the sentence I need to capitalize it.


Example



4 dogs were chasing 3 cats.
The 8 eggs were separated into 3 groups.


Becomes:



four dogs were chasing three cats.
The eight eggs were separated into three groups.


Code



public String ConvertSentance(String s){
sb = new StringBuilder();

h.put("0","zero");
h.put("1","one");
h.put("2", "two");
h.put("3", "three");
h.put("4", "four");
h.put("5", "five");
h.put("6", "six");
h.put("7", "seven");
h.put("8", "eight");
h.put("9", "nine");

String[] split = s.split(" ");

for (String newS : split) {
if (h.containsKey(newS)) {
sb.append(h.get(newS));
sb.append(' ');
}
else {
sb.append(newS);
sb.append(' ');
}
}

convertedSent = sb.toString();
return sb.toString();

}


How could I get this to capitalize the beginning character so that the output turns into this?



Four dogs were chasing three cats.


I am having trouble getting this to work only when they number is at the beginning of the sentence. I have tried different variations of for-loops with no success.




Aucun commentaire:

Enregistrer un commentaire