Im делает немного работы на дому java, и у меня, кажется, небольшая проблема. Проблема im, являющаяся переменной, которую Im пытается ссылаться, показывает, что она не инициализирована. Тем не менее, я объявил переменную ранее в методе, а затем инициализировал ее в цикле. Когда я пытаюсь получить доступ к переменной, когда я заставляю charCount вызывать несколько строк позже в том же методе, компилятор жалуется, что переменная все еще нуждается в инициализации. Может кто-нибудь объяснить, почему это не работает, как мне кажется.Переменная недоступна
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class test {
public int charCountHelper(File handle, Character x) throws IOException {
int count = 0;
String data;
int index;
Character[] contents;
Scanner inputFile = new Scanner(handle);
while(inputFile.hasNext()){
data=inputFile.nextLine();
index = data.length()-1;
for(int i = 0; i< data.length(); i++){
contents = new Character[data.length()] ;
contents[i] = data.charAt(i);
}
count += charCount(contents,x,index);
}
inputFile.close();
return count;
}
public int charCount(Character[] content, Character x, int index) {
if(index < 0){
return 0; // this value represents the character count if the program reaches the beginning of the array and has not found a match.
}
if (content[index].equals(x)) {
return 1 + charCount(content, x, index - 1);
}
return charCount(content, x, index - 1); // this is the value that gets returned to the original calling method.
}
}
Какая переменная? – Raedwald
count + = charCount (содержимое, x, индекс); Переменная содержимого. – Keith