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

    Question Money Conversion - Proofreading pls?

    Hello everyone.

    What I have right here is my Money Conversion program.

    I just want to ask for help to fix my codes. Sorry if it's kinda long.

    Errors found during runtime:
    1. it is partially incorrect when converting an input number with ones' digit
    2. Program terminates when user input is "5..2"


    Java Code:
    /*
     	* Description: A Java Program which will except a numeric input (double data type) from a user. Range input is from 0 - 99,999,99.	
     	*			   The main function of the program is to convert the input number to equivalent words. After displaying converted number,
     	*			   the program will ask the user if he/she wants to continue accessing the 	application.
    */
    
    public class MoneyConversion{
    	
    /*
    	 * Function Name: Main
     	 * Description: A Java Program which will except a numeric input (double data type) from a user. Range input is from 0 - 99,999,99.	
     	 *			   The main function of the program is to convert the input number to equivalent words. After displaying converted number,
     	 *			   the program will ask the user if he/she wants to continue accessing the 	application.
    	 * Arguments: String args[]
    	 * Return Type: none
     	 * Known Bugs:
     	 *	1. Inaccuracy on displaying tenths place literals.
     	 *  2. Some cases of set of numbers cannot be verified by the program. (returns a value of zero)		 
    */
    	public static void main(String args[])throws Exception{		
    		Information objWelcome=new Information();
    		objWelcome.DisplayMessage();
    		
    		String strContinue;
    		String strAmount="";
    		
    		do{
    			String strInput;
    			double dAmount=0;
    			int iPeriodIndex=0;
    			strInput = ReaderData.getStringInput("Enter an amount: ");
    		
    			if (strInput.trim().equals("")){
    				System.out.println("There is no input.\n");
    			}		
    		
    			dAmount=Double.parseDouble(strInput);
    			strInput = String.valueOf(dAmount);
    			iPeriodIndex = strInput.indexOf("."); 
    		
    			String strPesos = strInput.substring(0,iPeriodIndex);
    			String strCents = strInput.substring(iPeriodIndex + 1,strInput.length());
    		
    			MoneyInfo objProject=new MoneyInfo();
    		
    			if (strCents.length() ==1){
    					strCents = strCents + "0";
    			}
    			
    			if(dAmount >= 0 & Integer.parseInt(strInput.substring(iPeriodIndex + 1,strInput.length())) ==0 ){
    				System.out.println("Your amount input: " + 
    					objProject.getPesoValue(strPesos));
    			}
    			
    			// for single input
    			else if (dAmount < 1) {
    				System.out.println("Your amount input: " + 
    					objProject.getCentavoValue(strCents) );
    			}
    			
    			// for multiple inputs
    			else {
    				System.out.println("Your amount input: " + 
    					objProject.getPesoValue(strPesos) + " and " + 
    					objProject.getCentavoValue(strCents));
    			}
    		
    			System.out.println();
    			
    			if (strAmount.equals("")) {
    				strAmount= strInput;
    			}
    			
    			else {
    				strAmount= strAmount + ", "+ strInput;
    			}
    		strContinue=ReaderData.getStringInput("Do you want to continue? [y/n]: ");
    		if (strContinue.equals("n")||strContinue.equals("N"))
    		{
    		System.out.println("Your input amounts is/are the follwing: "+strAmount);
    		System.out.println("Goodbye. Thank you very much for using this application.");	
    		}
    		
    	}
    	while(strContinue.equals("y")||strContinue.equals("Y"));
    	}
    
    }
    
    class Information{
    	
    	/*
    	 *	Function Name: DisplayMessage
    	 *	Description: The primary role of this function to display the following instructions on using the 'MoneyConversion' program.
    	 *				 This is the first set of information that will display on the program.
    	 *	Arguments: none
    	 *	Return Type: none
    	 *  Known Bugs: none
    */	
    	public void DisplayMessage(){
    		System.out.println("\t\tMoney Conversion 1.5 - A Java Program"+ "\n");
    		System.out.println("Directions: \n"+"\t1.Input values from 0 to 999,999 that you wish to convert. \n\t2. Press enter."
    			+ "\n\t"	+"3. Numeric value you have inputted will be converted into word value/s.         4.End.\n");
    	}
    }
    
    class MoneyInfo{
    
    /*
    	 *	Function Name: getOnes
    	 *	Description: For ones value.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */
    public String getOnes(String strPesos){
    		if (strPesos.equals("1")){
    			return "one";
    		}
    		
    		else if(strPesos.equals("2")) {
    			return "two";
    		}
    		
    		else if(strPesos.equals("3")) {
    			return "three";
    		}
    		
    		else if(strPesos.equals("4")) {
    			return "four";
    		}
    		
    		else if(strPesos.equals("5")) {
    			return "five";
    		}
    		
    		else if(strPesos.equals("6")) {
    			return "six";
    		}
    		
    		else if(strPesos.equals("7")) {
    			return "seven";
    		}
    		
    		else if(strPesos.equals("8")) {
    			return "eight";
    		}
    		
    		else if(strPesos.equals("9")) {
    			return "nine";
    		}
    		else{
    			return "";
    		}
    	}
    	/*
    	 *	Function Name: getTens
    	 *	Description: For tens value.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */	
    	public String getTens(String strPesos){
    		if(strPesos.equals("2")) {
    			return "twenty";
    		}
    		else if(strPesos.equals("3")){
    			return "thirty";
    		}
    		else if(strPesos.equals("4")) {
    			return "forty";
    		}
    		
    		else if(strPesos.equals("5")) {
    			return "fifty";
    		}
    		
    		else if(strPesos.equals("6")) {
    			return "sixty";
    		}
    		
    		else if(strPesos.equals("7")) {
    			return "seventy";
    		}
    		
    		else if(strPesos.equals("8")) {
    			return "eighty";
    		}
    		
    		else if(strPesos.equals("9")) {
    			return "ninety";
    		}
    		else {
    			return "";
    		}
    	}
    /*
    	 *	Function Name: getHundreds
    	 *	Description: For hundreds value.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */
    	public String getHundreds(String strPesos){
    		if (strPesos.equals("1")){
    			return "one hundred";
    		}
    		
    		else if(strPesos.equals("2")) {
    			return "two hundred";
    		}
    		
    		else if(strPesos.equals("3")) {
    			return "three hundred";
    		}
    		
    		else if(strPesos.equals("4")) {
    			return "four hundred";
    		}
    		
    		else if(strPesos.equals("5")) {
    			return "five hundred";
    		}
    		
    		else if(strPesos.equals("6")) {
    			return "six hundred";
    		}
    		
    		else if(strPesos.equals("7")) {
    			return "seven hundred";
    		}
    		
    		else if(strPesos.equals("8")) {
    			return "eight hundred";
    		}
    		
    		else if(strPesos.equals("9")) {
    			return "nine hundred";
    		}
    		
    		else{
    			return"";
    		}
    	}
    /*
    	 *	Function Name: getThousands
    	 *	Description: For thousands value.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */		
    	public String getThousands(String strPesos){
    		if (strPesos.equals("1")){
    			return "one thousand";
    		}
    		
    		else if(strPesos.equals("2")) {
    			return "two thousand";
    		}
    		
    		else if(strPesos.equals("3")) {
    			return "three thousand";
    		}
    		
    		else if(strPesos.equals("4")) {
    			return "four thousand";
    		}
    		
    		else if(strPesos.equals("5")) {
    			return "five thousand";
    		}
    		
    		else if(strPesos.equals("6")) {
    			return "six thousand";
    		}
    		
    		else if(strPesos.equals("7")) {
    			return "seven thousand";
    		}
    		
    		else if(strPesos.equals("8")) {
    			return "eight thousand";
    		}
    		
    		else if(strPesos.equals("9")) {
    			return "nine thousand";
    		}
    		
    		else if (strPesos.equals("0")){
    			return "thousand";
    		}
    		
    		else {
    			return "";
    		}
    	}
    /*
    	 *	Function Name: getPesoValue
    	 *	Description: To get the peso computation.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */
    	public String getPesoValue(String strPesos){
    		String strCount="";
    		int iLength= strPesos.length();
    		MoneyInfo objProject=new MoneyInfo();
    		
    		for (int iCounter=0;iCounter<strPesos.length();iCounter++){
    			if(iLength==1){
    			if(strCount=="")
    				{
    					strCount=objProject.getOnes(strPesos.substring(iCounter,iCounter+1));
    				}
    			
    				else if (strPesos.substring(iCounter - 1,iCounter).equals("1")) {
    						strCount = strCount + objProject.getOnes("");	
    				}
    				
    				else {
    						strCount = strCount + " "+objProject.getOnes(strCount.substring(iCounter,iCounter+1));
    				}		
    			}
    				else if (iLength==2) {
    				if (strCount == "") {
    				if(Integer.parseInt(strPesos.substring(iCounter,iCounter+1)) == 0) {
    					continue;
    				}
    				else {
    						strCount = objProject.getTenth(strPesos.substring(iCounter+1,iCounter+2));
    					}	
    				}
    				else {
    					if(Integer.parseInt(strPesos.substring(iCounter,iCounter+1)) != 1) {
    						strCount = strCount + " " +objProject.getTens(strPesos.substring(iCounter,iCounter+1));
    					}
    				else {
    						strCount = strCount +" "+objProject.getTenth(strPesos.substring(iCounter+1,iCounter+2));
    					}	
    				}		
    			}
    			else if (iLength== 3) {
    				if (strCount == "") {
    					if(strPesos.substring(iCounter+1,iCounter+2).equals("0")) {
    						strCount = objProject.getHundreds(strPesos.substring(iCounter,iCounter+1));
    						iLength = 2;
    						iCounter=4;
    					}
    					
    					else {
    						strCount = objProject.getHundreds(strPesos.substring(iCounter,iCounter+1));
    					}	
    				}
    				else {
    					if(strPesos.substring(iCounter+1,iCounter+2).equals("0")) {
    						strCount = strCount + " "
    						+objProject.getHundreds(strPesos.substring(iCounter,iCounter+1));
    						iLength =2;
    						iCounter=4;
    					}
    					
    					else {
    						strCount = strCount + " "
    						+ objProject.getHundreds(strPesos.substring(iCounter,iCounter+1));
    					}
    				}		
    			}
    			else if (iLength== 4) {
    				if (strCount == "") {
    					if (strPesos.substring(iCounter+1,iCounter+4).equals("000")){
    						strCount = objProject.getThousands(strPesos.substring(iCounter,iCounter+1));
    						iLength = 2;
    						iCounter=4;
    					}
    					
    					else {
    						strCount = objProject.getThousands(strPesos.substring(iCounter,iCounter+1));
    					}
    				}
    				else {
    					if (strPesos.substring(iCounter+1,iCounter+4).equals("000")){
    						strCount = strCount + " " 
    						+ objProject.getThousands(strPesos.substring(iCounter,iCounter+1));
    						iLength = 2;
    						iCounter=4;
    					}
    					
    					else {
    						strCount = strCount + " " 
    						+objProject.getThousands(strPesos.substring(iCounter,iCounter+1));
    					}
    				}
    			}		
    			
    			else if (iLength >6) {
    				strCount = "error! out of range.";
    			}
    			
    			else {
    				continue;
    			}
    			
    			iLength--;
    		}
    		
    		if(strCount.equals("one")) {
    			return strCount + " peso";
    		}
    		
    		else if (strCount.equals("")){
    			return "zero";
    		}
    		
    		else if (strCount.equals("error! out of range.")){
    			return "Error! Cannot convert. Out of range.";
    		}
    		
    		else {
    			return strCount+ " pesos";
    		}
    		
    	}			
    /*
    	 *	Function Name: getTenths
    	 *	Description: For tenths of decimal value.
    	 *	Arguments: strPesos
    	 *	Return Type: none
    */
    	public String getTenth(String strPesos){
    		if(strPesos.equals("1")) {
    			return "eleven";
    		}
    		else if(strPesos.equals("0")){
    			return "ten";
    		}
    		else if(strPesos.equals("2")) {
    			return "twelve";
    		}
    		
    		else if(strPesos.equals("3")) {
    			return "thirteen";
    		}
    		
    		else if(strPesos.equals("4")) {
    			return "fourteen";
    		}
    		
    		else if(strPesos.equals("5")) {
    			return "fifteen";
    		}
    		
    		else if(strPesos.equals("6")) {
    			return "sixteen";
    		}
    		
    		else if(strPesos.equals("7")) {
    			return "seventeen";
    		}
    		
    		else if(strPesos.equals("8")) {
    			return "eighteen";
    		}
    		
    		else if(strPesos.equals("9")) {
    			return "nineteen";
    		}
    		else {
    			return "";
    		}
    	}
    	
    	public String getCentavoValue(String strPesos) {
    		String strCents="";
    		MoneyInfo objProject = new MoneyInfo();		
    
    		for(int iCounter=0;iCounter<strPesos.length(); iCounter++) {
    			if (iCounter == 0) {
    				if(Integer.parseInt(strPesos.substring(iCounter,iCounter+1)) == 0) {
    					continue;
    				}
    				else if (Integer.parseInt(strPesos.substring(iCounter,iCounter+1)) != 1) {
    					if(Integer.parseInt(strPesos.substring(iCounter+1,iCounter+2)) != 0) {
    						strCents = objProject.getTens(strPesos.substring(iCounter,iCounter+1)) + "-";
    					}
    					else {
    						strCents = objProject.getTens(strPesos.substring(iCounter,iCounter+1));
    					}
    				}
    				else {							
    					strCents = objProject.getTenth(strPesos.substring(iCounter+1,iCounter+2));
    				}					
    			}
    			
    			else {
    				if (strPesos.substring(iCounter-1,iCounter).equals("1")) {
    					strCents = strCents + objProject.getOnes("");	
    				}
    				else {
    					strCents = strCents+objProject.getOnes(strPesos.substring(iCounter,iCounter+1));
    				}		
    			}
    		} if(strCents.equals("one")) {
    			return strCents + " centavo";
    		}
    		
    		else {
    			return strCents+ " centavos";
    		}
    	}
    
    }

    Really sorry for the long code/s.


    You might wanna suggest me something so I could able to run this program properly. Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class MCRx{
        /*
         * Arguments: String[] args
         * Return Type: none
         * Known Bugs:
         * 1. Inaccuracy on displaying tenths place literals.
         * 2. Some cases of set of numbers cannot be verified by
         *    the program. (returns a value of zero)		 
         */
        public static void main(String args[])throws Exception{
            double[] amts = { 10.25, 25.0, 30.12, 75.20 };
            for(int i = 0; i < amts.length; i++) {
                String strAmount="";
                double dAmount = amts[i];
                System.out.println("Parsing " + dAmount);
                String strInput = String.valueOf(dAmount);
                int iPeriodIndex = strInput.indexOf("."); 
    
                String strPesos = strInput.substring(0,iPeriodIndex);
                String strCents = strInput.substring(iPeriodIndex + 1,
                                                     strInput.length());
    
                MoneyInfo objProject=new MoneyInfo();
    
                if (strCents.length() ==1){
                    strCents = strCents + "0";
                }
    
                if(dAmount >= 0 & Integer.parseInt(strInput.substring(
                                      iPeriodIndex + 1,strInput.length())) ==0 ){
                    System.out.println("Your amount input: " + 
                    objProject.getPesoValue(strPesos));
                } else if (dAmount < 1) {         // for single input
                    System.out.println("Your amount input: " + 
                    objProject.getCentavoValue(strCents) );
                } else {                          // for multiple inputs
                    System.out.println("Your amount input: " + 
                    objProject.getPesoValue(strPesos) + " and " + 
                    objProject.getCentavoValue(strCents));
                }
    
                System.out.println();
                if (strAmount.equals("")) {
                    strAmount= strInput;
                } else {
                    strAmount= strAmount + ", "+ strInput;
                }
            }
        }
    }
    
    class Information{
        /*
         * Function Name: DisplayMessage
         * Description: The primary role of this function to display the
         * following instructions on using the 'MoneyConversion' program.
         * This is the first set of information that will display on the program.
         * Arguments: none
         * Return Type: none
         * Known Bugs: none
         */	
        public void DisplayMessage(){
            System.out.println("\t\tMoney Conversion 1.5 - A Java Program"+ "\n");
            System.out.println("Directions: \n"+"\t1.Input values from " +
                               "0 to 999,999 that you wish to convert. " +
                               "\n\t2. Press enter." +
                               "\n\t"+"3. Numeric value you have inputted " +
                               "will be converted into word value/s. 4.End.\n");
        }
    }
    
    class MoneyInfo{
        /*
         * Function Name: getOnes
         * Description: For ones value.
         * Arguments: strPesos
         * Return Type: none
         */
        public String getOnes(String strPesos){
            if (strPesos.equals("1")){
                return "one";
            } else if(strPesos.equals("2")) {
                return "two";
            } else if(strPesos.equals("3")) {
                return "three";
            } else if(strPesos.equals("4")) {
                return "four";
            } else if(strPesos.equals("5")) {
                return "five";
            } else if(strPesos.equals("6")) {
                return "six";
            } else if(strPesos.equals("7")) {
                return "seven";
            } else if(strPesos.equals("8")) {
                return "eight";
            } else if(strPesos.equals("9")) {
                return "nine";
            } else{
                return "";
            }
        }
    
        /*
         * Function Name: getTens
         * Description: For tens value.
         * Arguments: strPesos
         * Return Type: none
         */	
        public String getTens(String strPesos){
            if(strPesos.equals("2")) {
                return "twenty";
            } else if(strPesos.equals("3")){
                return "thirty";
            } else if(strPesos.equals("4")) {
                return "forty";
            } else if(strPesos.equals("5")) {
                return "fifty";
            } else if(strPesos.equals("6")) {
                return "sixty";
            } else if(strPesos.equals("7")) {
                return "seventy";
            } else if(strPesos.equals("8")) {
                return "eighty";
            } else if(strPesos.equals("9")) {
                return "ninety";
            } else {
                return "";
            }
        }
    
        /*
         * Function Name: getHundreds
         * Description: For hundreds value.
         * Arguments: strPesos
         * Return Type: none
         */
        public String getHundreds(String strPesos){
            if (strPesos.equals("1")){
                return "one hundred";
            } else if(strPesos.equals("2")) {
                return "two hundred";
            } else if(strPesos.equals("3")) {
                return "three hundred";
            } else if(strPesos.equals("4")) {
                return "four hundred";
            } else if(strPesos.equals("5")) {
                return "five hundred";
            } else if(strPesos.equals("6")) {
                return "six hundred";
            } else if(strPesos.equals("7")) {
                return "seven hundred";
            } else if(strPesos.equals("8")) {
                return "eight hundred";
            } else if(strPesos.equals("9")) {
                return "nine hundred";
            } else{
                return"";
            }
        }
    
        /*
         * Function Name: getThousands
         * Description: For thousands value.
         * Arguments: strPesos
         * Return Type: none
         */		
        public String getThousands(String strPesos){
            if (strPesos.equals("1")){
                return "one thousand";
            } else if(strPesos.equals("2")) {
                return "two thousand";
            } else if(strPesos.equals("3")) {
                return "three thousand";
            } else if(strPesos.equals("4")) {
                return "four thousand";
            } else if(strPesos.equals("5")) {
                return "five thousand";
            } else if(strPesos.equals("6")) {
                return "six thousand";
            } else if(strPesos.equals("7")) {
                return "seven thousand";
            } else if(strPesos.equals("8")) {
                return "eight thousand";
            } else if(strPesos.equals("9")) {
                return "nine thousand";
            } else if (strPesos.equals("0")){
                return "thousand";
            } else {
                return "";
            }
        }
    
        /*
         * Function Name: getPesoValue
         * Description: To get the peso computation.
         * Arguments: strPesos
         * Return Type: none
         */
        public String getPesoValue(String strPesos){
            String strCount="";
            int iLength= strPesos.length();
            MoneyInfo objProject=new MoneyInfo();
    
            for (int iCounter=0; iCounter < strPesos.length(); iCounter++){
                if(iLength==1){
                    if(strCount==""){
                        strCount=objProject.getOnes(
                            strPesos.substring(iCounter,iCounter+1));
    //                } else if (strPesos.substring(iCounter - 1,
    //                                              iCounter).equals("1")) {
    //                    strCount = strCount + objProject.getOnes("");	
                    } else {
                        strCount = strCount + " " +
                                   objProject.getOnes(
    //                                   strCount.substring(iCounter,iCounter+1));
                                       strPesos.substring(iCounter,iCounter+1));
                    }
                } else if (iLength==2) {
                    if (strCount == "") {
                        int firstDigit = Integer.parseInt(
                                strPesos.substring(iCounter,iCounter+1));
                        if(firstDigit == 0) {
                            continue;
                        } else if(firstDigit == 1) {        // teens
                            // Parse the second digit for the teen.
                            strCount = objProject.getTenth(
                                strPesos.substring(iCounter+1,iCounter+2));
                        } else {                            // tens
                            strCount = objProject.getTens(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    } else {
                        if(Integer.parseInt(
                                strPesos.substring(iCounter,iCounter+1)) != 1) {
                            strCount = strCount + " " + objProject.getTens(
                                    strPesos.substring(iCounter,iCounter+1));
                        } else {
                            strCount = strCount +" "+ objProject.getTenth(
                                strPesos.substring(iCounter+1,iCounter+2));
                        }
                    }
                } else if (iLength== 3) {
                    if (strCount == "") {
                        if(strPesos.substring(iCounter+1,iCounter+2).equals("0")) {
                            strCount = objProject.getHundreds(
                                strPesos.substring(iCounter,iCounter+1));
                            iLength = 2;
                            iCounter=4;
                        } else {
                            strCount = objProject.getHundreds(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    } else {
                        if(strPesos.substring(iCounter+1,iCounter+2).equals("0")) {
                            strCount = strCount + " "+objProject.getHundreds(
                                strPesos.substring(iCounter,iCounter+1));
                            iLength =2;
                            iCounter=4;
                        } else {
                            strCount = strCount + " "+ objProject.getHundreds(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    }
                } else if (iLength== 4) {
                    if (strCount == "") {
                        if (strPesos.substring(iCounter+1,
                                               iCounter+4).equals("000")){
                            strCount = objProject.getThousands(
                                strPesos.substring(iCounter,iCounter+1));
                            iLength = 2;
                            iCounter = 4;
                        } else {
                            strCount = objProject.getThousands(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    } else {
                        if (strPesos.substring(iCounter+1,
                                               iCounter+4).equals("000")){
                            strCount = strCount + " " + objProject.getThousands(
                                strPesos.substring(iCounter,iCounter+1));
                            iLength = 2;
                            iCounter = 4;
                        } else {
                            strCount = strCount + " " +objProject.getThousands(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    }
                } else if (iLength >6) {
                    strCount = "error! out of range.";
    //            } else {
    //                continue;
                }
                iLength--;
            }
    
            if(strCount.equals("one")) {
                return strCount + " peso";
            } else if (strCount.equals("")){
                return "zero";
            } else if (strCount.equals("error! out of range.")){
                return "Error! Cannot convert. Out of range.";
            } else {
                return strCount+ " pesos";
            }
        }
    
        /*
         * Function Name: getTenths
         * Description: For tenths of decimal value.
         * Arguments: strPesos
         * Return Type: none
         */
        public String getTenth(String strPesos){
            if(strPesos.equals("1")) {
                return "eleven";
            } else if(strPesos.equals("0")){
                return "ten";
            } else if(strPesos.equals("2")) {
                return "twelve";
            } else if(strPesos.equals("3")) {
                return "thirteen";
            } else if(strPesos.equals("4")) {
                return "fourteen";
            } else if(strPesos.equals("5")) {
                return "fifteen";
            } else if(strPesos.equals("6")) {
                return "sixteen";
            } else if(strPesos.equals("7")) {
                return "seventeen";
            } else if(strPesos.equals("8")) {
                return "eighteen";
            } else if(strPesos.equals("9")) {
                return "nineteen";
            } else {
                return "";
            }
        }
    
        public String getCentavoValue(String strPesos) {
            String strCents="";
            MoneyInfo objProject = new MoneyInfo();		
    
            for(int iCounter=0;iCounter<strPesos.length(); iCounter++) {
                if (iCounter == 0) {
                    if(Integer.parseInt(
                            strPesos.substring(iCounter,iCounter+1)) == 0) {
                        continue;
                    } else if (Integer.parseInt(
                            strPesos.substring(iCounter,iCounter+1)) != 1) {
                        if(Integer.parseInt(
                                strPesos.substring(iCounter+1,iCounter+2)) != 0) {
                            strCents = objProject.getTens(
                                strPesos.substring(iCounter,iCounter+1)) + "-";
                        } else {
                            strCents = objProject.getTens(
                                strPesos.substring(iCounter,iCounter+1));
                        }
                    } else {							
                        strCents = objProject.getTenth(
                            strPesos.substring(iCounter+1,iCounter+2));
                    }
                } else {
                    if (strPesos.substring(iCounter-1,iCounter).equals("1")) {
                        strCents = strCents + objProject.getOnes("");	
                    } else {
                        strCents = strCents+objProject.getOnes(
                            strPesos.substring(iCounter,iCounter+1));
                    }
                }
            }
            if(strCents.equals("one")) {
                return strCents + " centavo";
            } else {
                return strCents+ " centavos";
            }
        }
    }

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Using float or double for money is a sin

    double dAmount=0;
    It leads to giant problems. Get out of the habit now. Use fixed point or BigDecimal.

  5. #5
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    thanks hardwire.

    however. i wish to use this code which allows user to input his own numbers to be converted.


    another thing, is there a way i could adjust the code in a way that if i subtract 20 to the existing number, the program will generate something like this

    user input: 35 pesos

    output: fifteen pesos
    ** 35 less 20

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Suggestion re the use of the if{}else if{} chains for the regular data(Strings from 1 to 9) in this program. Why not use an array to hold the strings to be returned. Something like the following:
    String[] tens = new String[] {"none", "ten", "twenty", ...
    int idx = strPesos.charAt(0) - '1'; // convert str 0-9 to int 0-9
    String tensName = tens[idx]; // get String value

Similar Threads

  1. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 07:42 AM
  2. Doc to Pdf conversion
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2009, 12:27 PM
  3. Replies: 1
    Last Post: 08-09-2008, 12:10 PM
  4. Conversion from wav to vox
    By bozovilla in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2008, 05:54 AM
  5. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 04:27 AM

Posting Permissions

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