dimanche 1 mars 2015

Getting "double" output -- Java HashMap



This is a spinoff of this question. I am using a HashMap to convert numbers 0-9 in a String to their text equivalent.


I am using this code to accomplish this:



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

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();

}

/**
* @return Returns the converted String
*/
public String getConvertedS(){
return convertedSent;
}


Output


Getting this:



The 8 eggs were separated into 3 groups.The eight eggs were separated into three groups.



When the output should be this:



The eight eggs were separated into three groups.



Any ideas how I can correct this? I have tried removing different things with no luck.


Here is the code for the full project.




Aucun commentaire:

Enregistrer un commentaire