| | |
Please Help!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
Convert Number to Words
Kindly Help me Explain this Line of Codes.
Line by Line if possible.
It was given to me by my friend.
Hoping for Some Help.
Thank you very much.
Kindly Help me Explain this Line of Codes.
Line by Line if possible.
It was given to me by my friend.
Hoping for Some Help.
Thank you very much.
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class WordNumber{ public static void main(String[]args){ for(int i = 0; i < 20; i++){ Scanner input = new Scanner(System.in); System.out.print("Enter a Non Floating Point Number: "); long x = input.nextLong(); System.out.println(makeWord(x)); //Please Explain }//end for }//end main //Please Explain private static final String[] numbers = { "zero","one","two","three","four","five","six", "seven","eight","nine","ten","eleven","twelve", "thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"}; private static final String[] tens = { "","teen","twenty","thirty","fourty","fifty", "sixty","seventy","eighty","ninety"}; //Please Explain public static String makeWord(long number){ String s = ""; if(number<0){ s="negative "; number*=-1; } s+=toWord(number); s = s.replace("-zero",""); return s; }//end of makeWord //Please Explain private static String toWord(long number){ if(number>=1000000000){ return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000); } else if(number>=1000000){ return toWord(number/1000000)+"-million-"+toWord(number%1000000); } else if(number>=1000){ return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000); } else if(number>=100){ return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);//Please Explain } else if(number>=20){ return tens[(int)number/10] + "-" + toWord(number%10); } else if(number>18){ return numbers[(int)number%10]+"teen"; } else{ return numbers[(int)number]; } }//end of toWord }//end class
Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?
In any case, if you had done your assignment, rather than copying it, you would probably understand it.
P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.
In any case, if you had done your assignment, rather than copying it, you would probably understand it.
P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?
In any case, if you had done your assignment, rather than copying it, you would probably understand it.
P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.
he explained some like this.
its a final variable which is String[] numbers and String[] tens.
static it is executed according to its appearance.
correct?
Java Syntax (Toggle Plain Text)
private static final String[] numbers = { "zero","one","two","three","four","five","six", "seven","eight","nine","ten","eleven","twelve", "thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"}; private static final String[] tens = { "","teen","twenty","thirty","fourty","fifty", "sixty","seventy","eighty","ninety"};
here's a static initializers which being executed in order.
right?
Java Syntax (Toggle Plain Text)
public static String makeWord(long number){ String s = ""; if(number<0){ s="negative "; number*=-1; } s+=toWord(number); s = s.replace("-zero",""); return s; }//end of makeWord private static String toWord(long number){ if(number>=1000000000){ return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000); } else if(number>=1000000){ return toWord(number/1000000)+"-million-"+toWord(number%1000000); } else if(number>=1000){ return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000); } else if(number>=100){ return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100); } else if(number>=20){ return tens[(int)number/10] + "-" + toWord(number%10); } else if(number>18){ return numbers[(int)number%10]+"teen"; } else{ return numbers[(int)number]; } }//end of toWord
kinda confused.
![]() |
Other Threads in the Java Forum
- Previous Thread: how to display a java message box on top of all active windows
- Next Thread: HELP!!!(please read this..)
Views: 231 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






