Results 1 to 3 of 3
Thread: Help needed in Java
- 07-08-2008, 12:22 PM #1
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
Help needed in Java
Description:The program basically lets the user input numbers from 0 - 99,999.9 which the program will convert the inputed number to it's equivalent words.
example
Input number: 3
World Equivalent: Three dollars
I have established to convert a ones digit.
Java Code:public class NumberConvert { /* *Function name: main *Description: This is where the main calls for a method(s). And from there it process * the codes and prints them. *Arguments: String args[] *Return Type: none *Known Bugs: */ public static void main(String args[]) throws Exception { System.out.println("Welcome to Money Conversion!" + "\n"); String strContinue; String strInputs = ""; do { Double dInput = ReaderData.getDoubleInput("Input number: "); String sInput = Double.toString(dInput); String WordEquivalent = ""; for(int iCtr = 3; iCtr <= sInput.length(); iCtr++) { if(iCtr == 3) { // OOP NumberConvert obj = new NumberConvert(); String Ones = sInput.substring((sInput.length() - iCtr), (sInput.length() - (iCtr - 1))); Ones = obj.getOnes(Ones); WordEquivalent = WordEquivalent + Ones; } } System.out.println("\n" + "Word Equivalent: " + WordEquivalent + " Dollars" + "\n"); if(strInputs.equals("")) { strInputs = WordEquivalent; } else { strInputs = strInputs + " , " + WordEquivalent; } strContinue = ReaderData.getStringInput("Do you want to continue? [Y/N]: "); System.out.println(); System.out.println(); if (strContinue.equals("n")) { System.out.println("\n" + "You have inputted: "+ strInputs + "\n" + "\n" + "\n" + "Thank you for using this program, Goodbye!" + "\n" + "\n"); } } while(strContinue.equals("y")); } /* *Method name: getOnes *Description: This method basically converts the numeric ones digit into it's equivalent words. * As you can see in the codes, it is quite simple how the algorithm works. It tells * that if this numeric input by the user is for example 1, it'll convert it to it's * default stored word, which is one. The same goes for the rest of the numbers. *Arguments: none *Return Type: ones *Known Bugs:none */ public String getOnes(String Ones) { int iConvert = Integer.parseInt(Ones); if (iConvert == 0) { Ones = "Zero"; } else if (iConvert == 1) { Ones = "One"; } else if (iConvert == 2) { Ones = "Two"; } else if (iConvert == 3) { Ones = "Three"; } else if (iConvert == 4) { Ones = "Four"; } else if (iConvert == 5) { Ones = "Five"; } else if (iConvert == 6) { Ones = "Six"; } else if (iConvert == 7) { Ones = "Seven"; } else if (iConvert == 8) { Ones = "Eight"; } else if (iConvert == 9) { Ones = "Nine"; } return Ones; } /* public String getTens() { // codes here } public String getHundreds() { // codes here } public String getThousands() { // codes here } */ }
------------------------
I'm still looking for a way to find the tens, hundreds and thosands value
eg.
45 = forty five dollars
123 = one hundred and twenty three dollars
3241 = three thousand and two hundred and forty one dollars
Any ideas? please help. :(
Thanks.
- 07-08-2008, 12:24 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Use a switch to do this not a stacked if else, much cleaner that way.
Also a switch for the other positions.
- 07-08-2008, 07:55 PM #3
switch almost built already
coverts to:Java Code:else if (iConvert == 9) { Ones = "Nine"; }
You have to place a break; on every statement, or this just becomes something you are not trying to accomplish.Java Code:switch( integer numeric value ) { case 0 : ( code omitted );// case 1 : ( code omitted );// case 2 : ( code omitted );// case 3 : ( code omitted );// case 4 : ( code omitted );// default: ( code omitted );// }Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Java Genius needed
By Sanguine.digitalis in forum Jobs OfferedReplies: 4Last Post: 06-29-2008, 11:10 PM -
Urgent Java programmer needed.
By KevinG in forum Jobs OfferedReplies: 5Last Post: 04-23-2008, 04:02 AM -
Java experts needed- 30 minute online Java projects
By michelle in forum Jobs OfferedReplies: 0Last Post: 03-05-2008, 11:47 PM -
Java help needed
By jeneal in forum New To JavaReplies: 0Last Post: 11-21-2007, 03:04 AM -
Java developers needed
By neoris in forum Jobs OfferedReplies: 0Last Post: 11-19-2007, 03:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks