Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help me solve this puzzle?

  • 26-11-2012 9:37am
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    I have a puzzle to solve where I have to write a small Java program that takes s input a number and outputs that word in numeric.

    E.g. 1 = one, 1001 = one thousand and one.

    I have written the program but have found a bug in it. The program is sometimes dropping the word 'and'.
    If I enter 1001, I get one thousand one (as opposed to one thousand and one). Same as 10001, I get ten thousand one ('and' missing again).

    If I enter 1101, I correctly get one thousand one hundred and one. This is because there is 1 as the second digit.

    Can anyone tell me how to resolve this issue? If spent hours at it and am stuck.
    final private static String[] units = {"zero","one","two","three","four",
    		"five","six","seven","eight","nine","ten",
    		"eleven","twelve","thirteen","fourteen","fifteen",
    		"sixteen","seventeen","eighteen","nineteen"};
    	
    	final private static String[] tens = {"","","twenty","thirty","forty","fifty",
    		"sixty","seventy","eighty","ninety"};
    
    	public String convert(Integer num){
    		
    		if( num < 20) return units[num];
    		if( num < 100) return tens[num/10] + ((num % 10 > 0)? " " + convert(num % 10):"");
    		if( num < 1000) return units[num/100] + " hundred" + ((num % 100 > 0)? " and " + convert(num % 100):"");
    		if( num < 100000) return convert(num / 1000) + " thousand" + ((num % 1000 > 0)? " " + convert(num % 1000):"") ;
    		return convert(num / 1000000) + " million" + ((num % 1000000 > 0)? " " + convert(num % 1000000):"") ;
    	}
    


Comments

  • Registered Users, Registered Users 2 Posts: 140 ✭✭Slashman X


    if( num < 1000) return units[num/100] + " hundred" + ((num % 100 > 0)? " and " + convert(num % 100):"");

    Change to >=


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    For 1001 only this line is used:

    if( num < 100000) return convert(num / 1000) + " thousand" + ((num % 1000 > 0)? " " + convert(num % 1000):"") ;

    it generates "one thousand" and then calls
    convert with number 1 which generates "one" so you get "one thousand one".

    The "and" part is generated only for numbers between 101 and 999 and that line is never called for numbers like 1001 or 10001.

    I'd suggest to make a flowchart to understand how your code works.


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    Slashman X wrote: »
    Change to >=

    That made no difference.


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    PrzemoF wrote: »

    it generates "one thousand" and then calls
    convert with number 1 which generates "one" so you get "one thousand one".

    The "and" part is generated only for numbers between 101 and 999 and that line is never called for numbers like 1001 or 10001.

    I had noticed that and that is the bit I cannot fix.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    On the thousand and million lines, check if num % is going to be less than 100 before you convert() it, if so, add " and ", if not don't.

    Also change your 4th 'if' to check for < 1 million rather than <100k, because your default case won't handle the hundreds of thousands.


  • Advertisement
  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    On the thousand and million lines, check if num % is going to be less than 100 before you convert() it, if so, add " and ", if not don't.

    Also change your 4th 'if' to check for < 1 million rather than <100k, because your default case won't handle the hundreds of thousands.

    It's working. Can't thank you enough Donkey.


  • Registered Users, Registered Users 2 Posts: 140 ✭✭Slashman X


    That made no difference.

    Ah my bad, read the code wrong. Stupid scrolling code boxes


  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    Try
    if (num < 100000)
       return convert(num / 1000) + " thousand"
    		+ ((num % 1000 > 0) ? ((num % 100 > 0) ? " and " : " ") + convert(num % 1000) : "");
    


Advertisement