-3

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?

buræquete
  • 14,226
  • 4
  • 44
  • 89
sfrank
  • 3
  • 1
  • 6
  • initialization within a while loop or a try block is not guaranteed to have happened. You must explicitly initialize local variables with some default value, and for a String consider using `""`. – Hovercraft Full Of Eels Apr 30 '17 at 03:18
  • Hint: The compiler does not realize that `System.exit(...)` calls do not return. – Stephen C Apr 30 '17 at 03:40

1 Answers1

0

IDE and Compiler shows it when it is not guaranteed initialization for safety/precaution. Do this and it should go away,

BufferedReader readerKeyword = null;
String key = null;

However, do make sure that it gets initialized.

It is not guaranteed initialization because it is inside a try-catch block and if you get an exception, you can get by without initialization because, you are handling it.

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • Would the downvoter explain anything? – Manish Kumar Sharma Apr 30 '17 at 03:18
  • It's a compile error, not some IDE warning. And a multiple-times-dupe. – pvg Apr 30 '17 at 03:18
  • @pvg : My bad, I also assumed, his IDE must be showing it too because Eclipse and Intellij do before compiler shows it. – Manish Kumar Sharma Apr 30 '17 at 03:20
  • @pulp_fiction I tried setting it equal to null, and it returns as null, so my output for System.out.println(key); is null, not the text in the files. – sfrank Apr 30 '17 at 03:27
  • @sfrank : I told you, setting it null is to bypass the compiler warning and that you have to ensure it actually gets initialized. You will have to debug the assignment statement to see why it is not getting initialized. – Manish Kumar Sharma Apr 30 '17 at 03:29
  • @pvg I mentioned in the OP that if certain parts of the code are removed then the variable is initialized. So MY question hasn't been answered several times. My question I guess becomes, am I using the StringBuffer appropriately here if it causing my string to no longer be initialized? – sfrank Apr 30 '17 at 03:30
  • @sfrank : Would you mind posting the parts that make the error go away? – Manish Kumar Sharma Apr 30 '17 at 03:32
  • Initialize it to a Null String: `String key = "";` since null is taboo in the location you're having the problem. You should also be carrying through with the **StringBuffer.append()** method in the line: `newCipher = sb.append(key + cipher).toString();`. It would be better as: `newCipher = sb.append(key).append(cipher).toString();`. – DevilsHnd - 退職した Apr 30 '17 at 03:36
  • @sfrank post a [MCVE] that shows the thing that reproduces your specific problem/unclear bit. – pvg Apr 30 '17 at 03:36
  • @pulp_fiction When I comment out the StringBuffer and the static String removeDuplicates(String newCipher), String key is initialized and set to the contents of the .txt file during the while loop. – sfrank Apr 30 '17 at 03:37
  • @sfrank : Go to ideone.com and post the code there then RUN it and copy the URL and post that here. – Manish Kumar Sharma Apr 30 '17 at 03:41
  • @DevilsHnd I also tried setting it to ""; and it also returned "null". I'm wondering if I need to handle the appending in a separate method other than main? – sfrank Apr 30 '17 at 03:43
  • @sfrank : That means, its getting initialized in the middle somewhere. You can debug to see exactly whats happening. – Manish Kumar Sharma Apr 30 '17 at 03:44
  • @pulp_fiction http://ideone.com/1TC5t6 I'm not familiar with this site. Do I need to create a .java file or point it to my file? – sfrank Apr 30 '17 at 03:46
  • @sfrank : Copy your code to there and run it. – Manish Kumar Sharma Apr 30 '17 at 03:47
  • @pulp_fiction ideone.com/1TC5t6 how does it know where to find my .txt files? Sorry I'm such a noob! – sfrank Apr 30 '17 at 03:51
  • Of course key = null, it's because your **while** loop wont end until it does. I believe what you want to do is reverse the characters of each line read in from file and display those lines in console is it not? Your cipher code should be in your while loop. I dunno..I may have this all wrong as to what you're trying to accomplish. – DevilsHnd - 退職した Apr 30 '17 at 03:52
  • @DevilsHnd Bingo!! There is was, thank you! I put the StringBuffer in the while loop and its perfect. Thanks again. – sfrank Apr 30 '17 at 03:59
  • @pulp_fiction thank you as well! I'll keep looking into ideone.com. seems like a very useful tool if I can figure it out. – sfrank Apr 30 '17 at 04:00