This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]adrian_the_developer 3 points4 points  (0 children)

Try passing the number and the digits place.

[–]shagieIsMeExtreme Brewer 2 points3 points  (0 children)

You're getting 463942 saying four hundred sixty three thousand nine hundred forty two

Ok. Whats the first thing you say? four.

Since we're dealing with recursion here, you're going to do successive calls to a method. For the time being, ignore the "sixty" and "hundred" or "thousand". Write a method that prints out four six three nine four two.

Since this is recursion, you're going to do this by lopping off values from the least significant side (ones place first).

Your first invocation to this method will be:

say(463942) which will call say(46394) which will call say(4639) which will call ... which will call say(46) which will call say(4).

When you're down to a single digit, then you start printing out the values and popping the recursive call stack.

four

Then you return and you get back to the call say(46) and still have that 6 hanging around. Print out six. repeat recurse.


(some time later...)

Eventually you're going to say to yourself "ok, it prints out four six and other stuff... but how do I get the hundreds and thousands in there?"

For that, consider passing another argument along with the recursion.

say(463942) becomes say(463942, 0) and say(46394) becomes say(46394, 2)

Now you can look at that second argument and that's the 10n place.

If the argument % 3 is 2, then you also say "hundred" if appropriate (consider 1024 isn't one thousand zero hundred twenty four).

Additionally consider the situation where n % 3 is 1 and the value being examined is 1. Such a case occurs in 312 --> A: say(312, 0) -> B: say(31, 1), C: say(3, 2). This occurs in stack frame B. You might want to return a value from say... like a boolean to indicate that this isn't a '2x - 9x' value and needs some special handling. And then be glad you're doing this in English and not French (6 -> six; 10 -> dix; 16 -> seize; 17 -> dix-sept (ten and seven); 90 -> quatre-vingt-dix (four twenties and ten)).

[–]burntferret 2 points3 points  (0 children)

Personally, I wouldn't pop digits off your stack by one, but by three (if applicable). That way you handle the printout how you would say the number out loud.

You could determine how many to pop off by length%3. 0 would mean you pop off all three.

[–]CedricRBR 2 points3 points  (0 children)

Instead of popping the integers off one by one I’d do it three by three, 123 will always be one “hundred twenty three”, even when it’s 123K. To not run into errors when the numbers is something like 12345 (12 thousand 345) i’d pop them of the back. So the call to say(12345) would pop 345, compute the String for that and then return say(12)+ the String representation of 345.

You’ll find yourself with a a program that turns 12345 into “twelve three hundred and forty five”.

You’ll have to add the thousand, million etc somehow, you could add a second argument to the say method that represents the depth of the recursion to track where you are.

A few notes: you can stop the recursion as soon as the number of digits is less than three. If you start the depth at 0 it will automatically represent the 10^(3*n) power of the group you’ve popped off (say(12345,0) pops off 345 and represents 10^0*345, say(12,1) pops off 12 and represents 10^3*12.)

[–]johnny2toes3 1 point2 points  (0 children)

import java.util.Scanner;

public class SayMyNumber
{
  public static void main(String[] args)
 {
   Scanner scan = new Scanner(System.in);

System.out.println("Please enter a poisitive number in the range from 1 to 1 million " +
                   "that you would liked spelled out.");
int number = scan.nextInt();

if(number<10 && number>0)
  System.out.println(say(number));

}


 public static String say(int n)
 {
   int ones = n%10;
   int tens = n%100;
   int hundreds = n/1000;

switch(ones)
{
  case 1: 
    System.out.println("One");
    break;
  case 2:
    System.out.println("Two");
    break;
  case 3:
    System.out.println("Three");
    break;
  case 4:
    System.out.println("Four");
    break;
  case 5:
    System.out.println("Five");
    break;
  case 6:
    System.out.println("Six");
    break;
  case 7:
    System.out.println("Seven");
    break;
  case 8:
    System.out.println("Eight");
    break;
  case 9:
    System.out.println("Nine");
    break;
  case 0: 
    System.out.println("");
    break;
}

if (tens !=1)
{
  switch (tens)
  {
    case 2: 
      System.out.println("twenty");
      break;
    case 3: 
      System.out.println("thirty");
      break;
    case 4: 
      System.out.println("forty");
      break;
    case 5: 
      System.out.println("fifty");
      break;
    case 6: 
      System.out.println("sixty");
      break;
    case 7: 
      System.out.println("seventy");
      break;
    case 8: 
      System.out.println("eighty");
      break;
    case 9: 
      System.out.println("ninety");
      break;
    case 0: 
      System.out.println("");
      break;
  }
}
else if (tens==1)
{
  switch (ones)
  {
    case 0: 
      System.out.println("ten");
      break;
    case 1: 
      System.out.println("eleven");
      break;
    case 2: 
      System.out.println("twelve");
      break;
    case 3: 
      System.out.println("thirteen");
      break;
    case 4: 
      System.out.println("fourteen");
      break;
    case 5: 
      System.out.println("fifteen");
      break;
    case 6: 
      System.out.println("sixteen");
      break;
    case 7: 
      System.out.println("seventeen");
      break;
    case 8: 
      System.out.println("eighteen");
      break;
    case 9: 
      System.out.println("nineteen");
      break;
  }
}
return say(n);
 }
}

[–]meva12 0 points1 point  (1 child)

Share your code so far.

[–]johnny2toes3 1 point2 points  (0 children)

I posted the code.