0

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).

5
  • Is the string always five letters...? In any event, if you're not allowed to use for loops, you'll simply have to do this for the first character, and the next, and the next -- five times, presumably. Commented Oct 1, 2024 at 21:16
  • 1
    The question (and tags) says "recursion" instead of loops, so I assume that OP wants that. The problem is that recursion is a lot harder than loops, so I don't understand why it would be correct here. I think the real answer is to clarify with your instructor. Please ask if recursion should be used here or if the instructor wants a different solution. (Louis told you how to do it without using either loops or recursion.)
    – markspace
    Commented Oct 1, 2024 at 21:22
  • You currently aren't using recursion. One problem is the expression used to index the array which could be outside the bounds of the array if longer string were passed in. Commented Oct 1, 2024 at 21:25
  • 1) why testing if (word.length() > 0)? Code returns if length is zero; otherwise, if not zero, it can only be greater than zero; 2) no need for (neither) casting in (int)wordL.charAt(0) - (int)'a' - the casting is automatic for subtraction
    – user85421
    Commented Oct 1, 2024 at 21:26
  • 3) I would create a recursive method that takes the text, an index and the result array; and an intermediate method that takes the text and creates the array to call the first method
    – user85421
    Commented Oct 1, 2024 at 21:41

2 Answers 2

2

All code paths must return a value, and the compiler doesn't know that a negative word length is impossible; it only knows that word.length() returns an int and that both if conditions may be false.

If fix, simplify your code to:

if (word.length() == 0) {
    ...
} else {
    ...
}
0
  • You haven't taken care to return when none of the conditions in your method are met.

  • You are only calculating the distance for one character, but you need to do it for all characters.

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);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.