0
No Numerals code coach question.
Need to find out some edge case.
3 odpowiedzi
+ 2
NITISH the key st contains only one character. It cannot match key "10".
Addendum: Be sure to recognize and skip cases when the number is greater than 10. I advise you to split the input string into words and determine whether the whole word is numeric, 1-10, else let it pass. You could use input.next() to read the input word by word.
+ 1
G'day NITISH would you mind editing your question to tag the language?
Does your for loop deliberately exclude i=str.length() or could that be your bug?
0
Map<String,String> map = new HashMap<String, String>();
map.put("0", "zero");
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
map.put("4", "four");
map.put("5", "five");
map.put("6", "six");
map.put("7", "seven");
map.put("8", "eight");
map.put("9", "nine");
map.put("10", "ten");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
String res = "";
if(str.length() <= 0){
return;
}
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
String st = Character.toString(ch);
if(st == " "){
res += " ";
}
else if(map.containsKey(st)){
res += st.replace(st, map.get(st));
}
else{
res += st;
}
}
System.out.println(res);