This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes From a given string of numeric values, print the individual numbers in words separated by space. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "From a given string of numeric values, print the individual numbers in words separated by space." Watch "From a given string of numeric values, print the individual numbers in words separated by space." New topic
Author

From a given string of numeric values, print the individual numbers in words separated by space.

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

From a given string of numeric values, print the individual numbers in words separated by space.
Input: “2491"

i have written the below prog for the same.


public class notoword {


public static void main(String[] args) {

int n=2346980;
int d;int rev=0;
while(n!=0){
d=n%10;

rev=rev*10+d;

n=n/10;

}
int num=rev;
while(num!=0){
d=num%10;
switch(d){

case 1:System.out.println("one");
break;
case 2:System.out.println("two");
break;
case 3:System.out.println("three");
break;
case 4:System.out.println("four");
break;
case 5:System.out.println("five");
break;
case 6:System.out.println("six");
break;
case 7:System.out.println("seven");
break;
case 8:System.out.println("eight");
break;
case 9:System.out.println("nine");
break;

}
num=num/10;
}

}

}

its not giving output for any string like 2340,9070.
I know I am dividing it by 10 so there is no output for this kind of input.
Please correct my programme as required.And can any one suggest any better solution for this.

Thanks in advance.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Aneek Banerjee wrote:From a given string of numeric values, print the individual numbers in words separated by space.
Input: “2491"

i have written the below prog for the same.



its not giving output for any string like 2340,9070.
I know I am dividing it by 10 so there is no output for this kind of input.
Please correct my programme as required.And can any one suggest any better solution for this.


First, what about zero? Can't zero appear in a number?

Second, is it really a good idea to reverse the number? What is the number 50 in reverse? What is the number 500 in reverse? What is the number 5000000? in reverse? Can you tell the difference?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
jQuery in Action, 2nd edition
 
subject: From a given string of numeric values, print the individual numbers in words separated by space.
 
Similar Threads
Digits in Array
Do while error, I'm going crazy
NullPointerException problems in Main
Study Material for Scanner and Fromatter
Largest/Greatest of n numbers