|
-
December 21st, 2011, 02:21 PM
#10
Re: Java Challenge
 Originally Posted by [email protected]
Hey guys here is the solution:
package com.test.jguru;
import java.util.Hashtable;
import java.util.Map;
public class ChallengeNumber {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer,String> mapNum = new Hashtable<Integer, String>();
mapNum.put(1, "one");
mapNum.put(2, "two");
mapNum.put(3, "three");
mapNum.put(4, "four");
mapNum.put(5, "five");
mapNum.put(6, "six");
mapNum.put(7, "seven");
mapNum.put(8, "eight");
mapNum.put(9, "nine");
String strNum = "10444221";
System.out.println("String Length "+strNum.length());
int count = 0;
for(int i = 0;i<strNum.length();i++){
if(!(i+1 == strNum.length())){
if(strNum.charAt(i) == strNum.charAt(i+1)){
count++;
}else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
count = 0;
}
} else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
}
}
}
}
This code will work for all the combinations
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|