AnaviLucas
AnaviLucas

Reputation: 41

Stuck with recursive method for Java?

I'm trying to create a method that will turn an integer value into a String of words. Here is my code:

    public static void num(int n)
     {  String[] numbers = {"one", "two", "three", "four", "five" , "six", "seven", "eight", "nine"};
    if(n==0)
         System.out.println(numbers[n]);
    else
    {
        String numStr = Integer.toString(n);
    }

I do not know how to proceed after this to call the method over again. An example run would be 1234 would then be converted to one two three four.

Upvotes: 1

Views: 101

Answers (2)

S.L. Gay
S.L. Gay

Reputation: 11

As Martín Muñoz del Río pointed out, there's no to convert the integer to a String because this will only create a string of numbers (i.e., 1234 becomes "1234"). It is also unhelpful for your if statement to check if the integer equals 0 because this will not address other single digit integers. If n equals 3 or 8, the program will simply move on to the next step.

Storing the string representation of each number in an array is a great first step, but you want to also create a String that will store the full string representation of the original integer. I've created and initialized the String digitString to do so.

    public static void num (int n) {

            String[] numbers = {"zero", "one", "two", "three", "four",      "five", "six", "seven", "eight", "nine"};

            String digitString = "";

Your original if statement had the right idea, but it only checked if the single-digit integer was zero. To fix this, adjust the if statement to check if n less than 10. Since a recursive method will need to remove the digits that it has already evaluated, this adjustment will also ensure that the program knows what to do when there is only one digit left.

    public static void num (int n) {

            String[] numbers = {"zero", "one", "two", "three", "four",      "five", "six", "seven", "eight", "nine"};

            String digitString = "";

            if (n < 10) {
                digitString += numbers[n] + " ";
            } else {
                int digit = n%10;
                digitString = numbers[digit] + " " + digitString;
                num(n/10);
            }

            System.out.print(digitString);
    }

If n is greater than or equal to 10, the else block of the statement will isolate the last digit of the integer by finding the remainder of n/10. Once the digit is isolated you can retrieve the string by using digit as an index in numbers. Add the string representation of the digit to digitString, but do not use += or the string will hold the digits in reverse order. To call the loop again without evaluating the same digit, plug n/10 into the method.

System.out.print() is used here instead of System.out.println() so that each string digit will print on the same line.

You can call the method in the main just as Martín Muñoz del Río displayed above:

    public static void main(String[] args) {
            num(3456);
    }

Hope this was helpful!

Upvotes: 1

Note that you are not including zero, and I not understand why are you doing: String numStr = Integer.toString(n);

What you are trying to do is something like:

public static void num(int n){
    String[] numbers = {"zero","one", "two", "three", "four", "five" , "six", "seven", "eight", "nine"};
    if(n<10){
         System.out.println(numbers[n]);
         return;
    }
    else{
         num(n/10);
         System.out.println(numbers[n%10]);
    }

}

You can use this method like:

public static void main(String args[]){
    num(1234);
}

Upvotes: 2

Related Questions