-1
package io;

import java.io.*;

public class UserIO {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char c;
        try {
            c = (char)br.read(); //Input a character from user
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("c: " + c);
    }
}

The program is unable to compile.

Error: The local variable c may not have been initialized

Turing85
  • 18,217
  • 7
  • 33
  • 58
Arpit Tomar
  • 187
  • 1
  • 8
  • 2
    So initialize it - `Char c = NULL;` – TDG Jul 11 '15 at 10:59
  • You're literally telling us the reason: The local variable c may not have been initialized. Initialize it. Otherwise the println() won't have a value to use in case the try block fails. – runDOSrun Jul 11 '15 at 11:01
  • 2
    @TDG, in Java, it's not `NULL`, it's `null`. – RealSkeptic Jul 11 '15 at 11:01
  • 2
    ^ and you cannot assign null to primitive types... – Codebender Jul 11 '15 at 11:03
  • Hint: the compiler sees that when you get an exception while reading the input, variable `c` would remain unassigned. You need assign it in the `catch`, assign it unconditionally, or not use `c` outside the `try` block. – Sergey Kalinichenko Jul 11 '15 at 11:07
  • 1
    possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – RealSkeptic Jul 11 '15 at 11:09

4 Answers4

1

Intialize char c = '';.

If your try block fails your character c will not have any value.

  try {
    c = (char)br.read(); //Input a character from user
  } catch (IOException e) {
    e.printStackTrace();
  }

The above code might not assign any value to c, if there is some Exception thrown within br.read(). So when to execute System.out.println("c: " + c);, the compiler does not know what to print. In order to avoid this, Java forces you to initialize local variables.

If you intialize c with char c = '';, even though your try block fails, the compiler can at least print ``.

You should be able to do something like :

 char c;
 c = 'a';
 System.out.println("c:" + c);

In this case it will compile successfully, because when you print the value compiler knows it will certainly have some value, which is not the case with your try block (it might or might not have any value).

Turing85
  • 18,217
  • 7
  • 33
  • 58
Karthik
  • 4,950
  • 6
  • 35
  • 65
0

Think about it: In case the try block fails, what will be printed out by System.out.println("c: " + c)? Well, whatever value c had before the try-block. What value did c last hold before the try block? Nothing, you didn't specify any prior value, the value is not initialized. Hence, the error message.

Solution: Give c a default value upon declaration (or in a finally block). One that can be used in case the try-block fails.

Turing85
  • 18,217
  • 7
  • 33
  • 58
runDOSrun
  • 10,359
  • 7
  • 47
  • 57
  • You might want to mention that this effect is due to the fact, that `c` is a local variable. If `c` were a field of an object, it would have a defaulf value since `char` is a primitive. – Turing85 Jul 11 '15 at 11:18
0

You need to actually read what the user is inputting after they have finish

        while ((c = (char) br.read()) != -1) {
               System.out.println("c: " + c);
        }
    }
}
HelloWorld
  • 133
  • 1
  • 15
  • 4
    Instead of simply posting code, please explain the problem OP had and the solution. *Give a man a fish, and he has food for a day. Teach a man to fish and he will never be hungry again*. – Turing85 Jul 11 '15 at 11:13
0

i dont know you still need this solution, but i wanna answer your ask. after catch exception, you could use "return;" to back, and in order to not break "c".

public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    char c;
    try {
        c = (char)br.read(); //Input a character from user
    } catch (IOException e) {
        e.printStackTrace();
`       return;
    }
    System.out.println("c: " + c);
}