I can not for the life of me figure out why I'm getting a "variable key might not have been initialized" error. I've entered my entire code because if I remove the BufferedReader and set the string equal to I don't get the error. Also, if I leave the BufferedReader part in and remove the StringBuffer part, string key initializes just fine. Please help! New to java (and programming in general).
import java.util.*;
import java.io.*;
public class reverseString {
public static void main(String [] args) {
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String cipher = "";
String input = "";
String newCipher;
String ouput = "";
BufferedReader readerKeyword = null;
String key;
try {
readerKeyword = new BufferedReader(new FileReader("keyword.txt"));
} catch (FileNotFoundException fnfex) {
System.out.println(fnfex.getMessage() + " File not found.");
System.exit(0);
}
try {
while ((key = readerKeyword.readLine()) !=null) {
System.out.println(key);
}
} catch (IOException ioex) {
System.out.println(ioex.getMessage() + " Unable to read file.");
System.exit(0);
}
StringBuffer sb = new StringBuffer();
int len = abc.length();
for(int i = len -1;i>=0;i--)
cipher = cipher + abc.charAt(i);
System.out.println(abc);
System.out.println(cipher);
newCipher = sb.append(key + cipher).toString();
System.out.println(newCipher);
System.out.println(removeDuplicates(newCipher));
}
static String removeDuplicates(String newCipher) {
char[] charArr = newCipher.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for(char ch : charArr) {
charSet.add(ch);
}
StringBuffer StrBuf = new StringBuffer();
for(char c : charSet) {
StrBuf.append(c);
}
return StrBuf.toString();
}
}
Was is not initialized in the while loop at line28?