javaarraysrecursion

How to insert the result of a recursive method into an array in Java?


I'm a beginner to Java and am having trouble figuring out what's wrong with my code here. I get an error message that says my method must return a result of type int[], but am not sure how to proceed. Essentially, what I'm trying to do in the distanceFromA method is take each character of a string, get its distance from the letter 'a' in the alphabet as an integer, and insert that integer into a 5-integer array in the order the characters appear in the string. What am I missing?

import java.util.Arrays;
public class WordDistances {
    public static int[] distanceFromA(String word) {
        if (word.length() == 0) {
            int[] error = {0,0,0,0,0};
            return error;
        }
        if (word.length() > 0) {
            int[] testArray = new int[5];
            String wordL = word.toLowerCase();        
            testArray[Math.abs(word.length()-5)] = (int)wordL.charAt(0) - (int)'a';
            return testArray;
        }
    }
    public static void main(String[] args) {
        System.out.print(Arrays.toString(distanceFromA("abcde")));
    }
}

I haven't learned how to use for loops yet, and am trying to complete it without using one, but all of the tutorials I've found unfortunately use them.

EDIT: The string must be 5 characters, and I do have to use recursion exclusively (no loops or just doing it 5 times, unfortunately).


Solution

  • If you want to implement a recursive function, you need to take care of three things.

    1. The base case.

    This is the condition under which the recursion stops. Without a base case, the recursion can continue indefinitely, which will lead to a call stack overflow. The base case is the simplest form of the problem that can be solved without further recursive calling.

    int length = source.length();
    if (length == 0) {
      return;
    }
    

    Your base case is receiving an empty string. The length variable contains the length of the string. If the length of the string is 0, then you terminate the function.

    2. The recursive case.

    This is the part of the function where the recursive call occurs. In this case, the original problem is broken down into one or more subproblems, the solution to which will be calculated using the same function. Each subproblem should bring us closer to the base case to avoid infinite recursion.

    int index = length - 1;
    result[index] = source.charAt(index) - 'a';
    calculate(source.substring(0, index), result);
    

    The index variable contains the number of the last character of the string, which is also the index of the last element of the result array. The last element of the array contains the distance of the last character. A recursive call is made for the string without the last character.

    3. Going to the base case.

    This is a mechanism for reducing or changing the input data, where each recursive call brings the function closer to the base case. This mechanism is necessary to ensure that the base case is eventually executed.

    source.substring(0, index)
    

    In your case, the input data is a string, and each recursive call reduces this string by one character.

    As a result, you get the code

    public void calculate(String source, int[] result) {
      int length = source.length();
      if (length == 0) {
        return;
      } 
      int index = length - 1;
      result[index] = source.charAt(index) - 'a';
      calculate(source.substring(0, index), result);
    }