Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2008, 01:22 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 19
javanewbie is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-08-2008, 01:24 PM
Senior Member
 
Join Date: Jun 2008
Posts: 161
masijade is on a distinguished road
Use a switch to do this not a stacked if else, much cleaner that way.

Also a switch for the other positions.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2008, 08:55 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
switch almost built already
Code:
else if (iConvert == 9) { Ones = "Nine"; }
coverts to:
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 );// }
You have to place a break; on every statement, or this just becomes something you are not trying to accomplish.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Genius needed Sanguine.digitalis Jobs Offered 4 06-30-2008 12:10 AM
Urgent Java programmer needed. KevinG Jobs Offered 5 04-23-2008 05:02 AM
Java experts needed- 30 minute online Java projects michelle Jobs Offered 0 03-06-2008 12:47 AM
Java help needed jeneal New To Java 0 11-21-2007 04:04 AM
Java developers needed neoris Jobs Offered 0 11-19-2007 04:48 PM


All times are GMT +3. The time now is 09:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org