Results 1 to 3 of 3
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Exclamation 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.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use a switch to do this not a stacked if else, much cleaner that way.

    Also a switch for the other positions.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default switch almost built already

    Java Code:
    else if (iConvert == 9)
    {
           Ones = "Nine";
    }
    coverts to:
    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 );//
    }
    You have to place a break; on every statement, or this just becomes something you are not trying to accomplish.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Java Genius needed
    By Sanguine.digitalis in forum Jobs Offered
    Replies: 4
    Last Post: 06-29-2008, 11:10 PM
  2. Urgent Java programmer needed.
    By KevinG in forum Jobs Offered
    Replies: 5
    Last Post: 04-23-2008, 04:02 AM
  3. Java experts needed- 30 minute online Java projects
    By michelle in forum Jobs Offered
    Replies: 0
    Last Post: 03-05-2008, 11:47 PM
  4. Java help needed
    By jeneal in forum New To Java
    Replies: 0
    Last Post: 11-21-2007, 03:04 AM
  5. Java developers needed
    By neoris in forum Jobs Offered
    Replies: 0
    Last Post: 11-19-2007, 03:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •