Foros del Web » Programación para mayores de 30 ;) » Java »

variable ... might not have been initialized

Estas en el tema de variable ... might not have been initialized en el foro de Java en Foros del Web. Hola, que tal, estoy estudiando un curso de Fundamentos de Programación en la carrera de Ing. en Sistemas, pero tengo un pequeño problema al correr ...
  #1 (permalink)  
Antiguo 23/10/2014, 22:47
 
Fecha de Ingreso: abril-2010
Mensajes: 4
Antigüedad: 14 años
Puntos: 0
variable ... might not have been initialized

Hola, que tal, estoy estudiando un curso de Fundamentos de Programación en la carrera de Ing. en Sistemas, pero tengo un pequeño problema al correr este programa.
Código:
import java.io.IOException;

public class programa1 {

    public static void main(String[] args) throws IOException {
        char x;
        byte i;
        boolean continuar, aux;
        do {
            System.out.print("Introduce un caracter: ");
            x = (char) System.in.read();
            System.in.read();
            System.out.println("Introdujiste el caracter: " + x);
            do {
                System.out.print("¿Continuar? (0 no, 1 si): ");
                i = (byte) System.in.read();
                System.in.read();
                if (i == 1) {
                    continuar = true;
                    aux = true;
                } else if (i == 0) {
                    continuar = false;
                    aux = true;
                } else {
                    System.out.println("Error");
                    aux = false;
                }
            } while (aux == false);

        } while (continuar == true);

    }
}
La consola me manda el siguiente error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable continuar might not have been initialized.
¿Como puedo solucionarlo? ya intenté cambiando el condicional if por un switch, e incluso le di un valor a la variable al momento de crearla, pero luego no me modifica su valor y ejecuta con el valor que le di por defecto. Muchas gracias

Última edición por agj1210; 23/10/2014 a las 22:54
  #2 (permalink)  
Antiguo 24/10/2014, 00:38
Avatar de Profesor_Falken  
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: variable ... might not have been initialized

Buenas,
La variables deben ser siempre explicitamente inicializadas antes de su uso cuando se declaran en ambito local.

http://docs.oracle.com/javase/tutori...datatypes.html
Cita:
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
En tu caso, no inicializas la variable continuar, y esta puede llegar hasta la instruccion
while (continuar == true);
Sin haber sido inicializada, ya que que le asignas el valor solo en ciertas condiciones.

Un saludo
__________________
If to err is human, then programmers are the most human of us
  #3 (permalink)  
Antiguo 24/10/2014, 01:14
 
Fecha de Ingreso: abril-2010
Mensajes: 4
Antigüedad: 14 años
Puntos: 0
Respuesta: variable ... might not have been initialized

Ya lo solucioné, tuve que modificar el código de la siguiente manera:
Código Java:
Ver original
  1. public class programa1 {
  2.  
  3.     public static void main(String[] args) throws IOException {
  4.         char x;
  5.         int i;
  6.         boolean continuar = true, aux;
  7.         while (continuar == true) {
  8.             System.out.print("Introduce un caracter: ");
  9.             x = (char) System.in.read();
  10.             System.in.read();
  11.             System.out.println("Introdujiste el caracter: " + x);
  12.             do {
  13.                 System.out.print("¿Continuar? (0 no, 1 si): ");
  14.                 i = (int) System.in.read();
  15.                 System.in.read();
  16.                 switch (i) {
  17.                     case '0':
  18.                         continuar = false;
  19.                         aux = false;
  20.                         break;
  21.                     case '1':
  22.                         continuar = true;
  23.                         aux = false;
  24.                         break;
  25.                     default:
  26.                         System.out.println("ERROR");
  27.                         aux = true;
  28.                         break;
  29.                 }
  30.             } while (aux == true);
  31.         }
  32.     }
  33. }
Igualmente, muchas gracias.
  #4 (permalink)  
Antiguo 24/10/2014, 01:22
Avatar de Profesor_Falken  
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: variable ... might not have been initialized

En realidad, solo con haber cambiado esta linea bastaria:

boolean continuar = true, aux;

Un salud
__________________
If to err is human, then programmers are the most human of us

Etiquetas: variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:45.