Java - JOptionPane if

 
Vista:

JOptionPane if

Publicado por kener (8 intervenciones) el 13/03/2020 20:56:06
hola que tal compañeros estoy empezando a utilizar el JOptionPane if
quiero mostrar los datos capturados

nombre vendedor no hay problema pero cuando quiero ingresar vacaciones muestra un error

si alguien me ayuda se los agradeceria

saludos


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import javax.swing.JOptionPane;
 
public class CuadrodeDialogo {
 
    public static void main(String[] args) {
        JOptionPane.showInternalMessageDialog(null, "HOLA BIENVENDO A EVALUACIONES MENSULAES");
        String mes = JOptionPane.showInputDialog(null, "INGRESE EL MES A EVALUAR");
 
        String nombre = JOptionPane.showInputDialog(null, "INGRESE EL NOMBRE DEL SUPERVISOR");
        JOptionPane.showInternalMessageDialog(null, "BIENVENDIO " + nombre);
 
        String vendedor = JOptionPane.showInputDialog(null, "INGRESE EL NOMBRE DEL VENDEDOR");
 
        int pregunta = JOptionPane.showInternalConfirmDialog(null, "QUIERES EVALUAR A ?" + vendedor);
 
        if (pregunta == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "correcto continuemos");
        }
        if (pregunta == JOptionPane.NO_OPTION) {
 
            JOptionPane.showInternalMessageDialog(null, "SALGA PARA INGRESAR VENDEDOR NUEVO");
 
        }
        if (pregunta == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showInternalMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
 
        String presu = JOptionPane.showInputDialog(null, "INGRESE EL PRESUPUESTO de " + vendedor);
        String venta = JOptionPane.showInputDialog(null, "CUANTO VENDIO " + vendedor + " EN " + mes);
 
        int pregfalta = JOptionPane.showInternalConfirmDialog(null, "TUVO FALTAS ?" + vendedor);
 
        if (pregfalta == JOptionPane.YES_OPTION) {
            String faltas = JOptionPane.showInputDialog(null, "INGRESE EL NUMERO DE FALTAS");
 
        }
        if (pregfalta == JOptionPane.NO_OPTION) {
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
        }
        if (pregfalta == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showInternalMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
        int pregretardo = JOptionPane.showInternalConfirmDialog(null, "TUVO RETARDOS ?" + vendedor);
 
        if (pregretardo == JOptionPane.YES_OPTION) {
            String retardos = JOptionPane.showInputDialog(null, "INGRESE LOS MINUTOS DE RETARDOS");
 
        }
        if (pregretardo == JOptionPane.NO_OPTION) {
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
        }
        if (pregretardo == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showInternalMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
        int pregvaca = JOptionPane.showInternalConfirmDialog(null, "TUVO VACACIONES ?" + vendedor);
 
        if (pregvaca == JOptionPane.YES_OPTION) {
            String vaca = JOptionPane.showInputDialog(null, "INGRESE LOS DIAS TOMADOS");
 
        }
        if (pregvaca == JOptionPane.NO_OPTION) {
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
        }
        if (pregvaca == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showInternalMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
 
        String presentacion = JOptionPane.showInputDialog(null, "CUAL ES LA EVALUACION DE " + vendedor + " 0,10,20 ");
 
        JOptionPane.showInputDialog(null, "NOMBRE DEL SUPERVISOR :" + nombre);
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + vendedor);
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + presentacion);
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + vaca);
 
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

JOptionPane if

Publicado por Kabuto (1381 intervenciones) el 13/03/2020 23:40:00
Hola.

El problema está en que tu declaras la variable para recoger las vacaciones, SOLAMENTE si el usuario responde Yes/Si

1
2
3
4
if (pregvaca == JOptionPane.YES_OPTION) {
    String vaca = JOptionPane.showInputDialog(null, "INGRESE LOS DIAS TOMADOS");
 
}

Pero, luego al final del programa, invocas a esta variable

1
2
3
4
JOptionPane.showInputDialog(null, "NOMBRE DEL SUPERVISOR :" + nombre);
JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + vendedor);
JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + presentacion);
JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + vaca);

Esto es inaceptable para el compilador, porque la existencia de esa variable esta condicionada a que el usuario responda que SI.

Si responde que NO, esa variable no existirá, y por lo tanto esa última línea del programa será imposible de ejecutar porque pretendería acceder a una variable que no existe.
Por eso el compilador no puede aceptarlo.

La solución es declarar esa variable fuera de toda condición para asegurar su existencia.
Y además, asegurarte de que va a tener algún valor informativo tanto si responde SI como si responde que NO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int pregvaca = JOptionPane.showInternalConfirmDialog(null, "TUVO VACACIONES ?" + vendedor);
 
String vaca = "";
if (pregvaca == JOptionPane.YES_OPTION) {
    vaca = JOptionPane.showInputDialog(null, "INGRESE LOS DIAS TOMADOS");
 
}
if (pregvaca == JOptionPane.NO_OPTION) {
    vaca = "No tuvo vacaciones.";
    JOptionPane.showMessageDialog(null, "correcto continuemos");
}
if (pregvaca == JOptionPane.OK_CANCEL_OPTION) {
    vaca = " No sabe/No contesta";
    JOptionPane.showInternalMessageDialog(null, "USUARIO NO RESPONDE");
 
}

Con esos cambios, el compilador ya da por bueno el código.

Por cierto, las últimas líneas, no deberían ser inputDialog, sino MessageDialog

1
2
3
4
JOptionPane.showMessageDialog(null, "NOMBRE DEL SUPERVISOR :" + nombre);
JOptionPane.showMessageDialog(null, "NOMBRE DEL VENDEDOR :" + vendedor);
JOptionPane.showMessageDialog(null, "NOMBRE DEL VENDEDOR :" + presentacion);
JOptionPane.showMessageDialog(null, "NOMBRE DEL VENDEDOR :" + vaca);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

JOptionPane if

Publicado por Costero (148 intervenciones) el 13/03/2020 23:58:11
Quitale los "Internal".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import javax.swing.*;
 
public class CuadroDeDialogo {
 
    public static void main(String[] args) {
 
        JOptionPane.showMessageDialog(null, "HOLA BIENVENDO A EVALUACIONES MENSULAES");
 
        String mes = JOptionPane.showInputDialog(null, "INGRESE EL MES A EVALUAR");
 
        String nombre = JOptionPane.showInputDialog(null, "INGRESE EL NOMBRE DEL SUPERVISOR");
 
        JOptionPane.showMessageDialog(null, "BIENVENDIO " + nombre);
 
        String vendedor = JOptionPane.showInputDialog(null, "INGRESE EL NOMBRE DEL VENDEDOR");
 
        int pregunta = JOptionPane.showConfirmDialog(null, "QUIERES EVALUAR A ?" + vendedor);
 
 
        if (pregunta == JOptionPane.YES_OPTION) {
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
 
        }
 
        if (pregunta == JOptionPane.NO_OPTION) {
 
            JOptionPane.showMessageDialog(null, "SALGA PARA INGRESAR VENDEDOR NUEVO");
 
        }
 
        if (pregunta == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
 
 
        String presu = JOptionPane.showInputDialog(null, "INGRESE EL PRESUPUESTO de " + vendedor);
 
        String venta = JOptionPane.showInputDialog(null, "CUANTO VENDIO " + vendedor + " EN " + mes);
 
 
        int pregfalta = JOptionPane.showConfirmDialog(null, "TUVO FALTAS ?" + vendedor);
 
 
        if (pregfalta == JOptionPane.YES_OPTION) {
 
            String faltas = JOptionPane.showInputDialog(null, "INGRESE EL NUMERO DE FALTAS");
 
 
        }
 
        if (pregfalta == JOptionPane.NO_OPTION) {
 
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
 
        }
 
        if (pregfalta == JOptionPane.OK_CANCEL_OPTION) {
 
 
            JOptionPane.showMessageDialog(null, "USUARIO NO RESPONDE");
 
 
        }
 
        int pregretardo = JOptionPane.showConfirmDialog(null, "TUVO RETARDOS ?" + vendedor);
 
 
        if (pregretardo == JOptionPane.YES_OPTION) {
 
            String retardos = JOptionPane.showInputDialog(null, "INGRESE LOS MINUTOS DE RETARDOS");
 
 
        }
 
        if (pregretardo == JOptionPane.NO_OPTION) {
 
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
 
        }
 
        if (pregretardo == JOptionPane.OK_CANCEL_OPTION) {
 
 
            JOptionPane.showMessageDialog(null, "USUARIO NO RESPONDE");
 
 
        }
 
        int pregvaca = JOptionPane.showConfirmDialog(null, "TUVO VACACIONES ?" + vendedor);
 
 
        if (pregvaca == JOptionPane.YES_OPTION) {
 
            String vaca = JOptionPane.showInputDialog(null, "INGRESE LOS DIAS TOMADOS");
 
 
        }
 
        if (pregvaca == JOptionPane.NO_OPTION) {
 
 
            JOptionPane.showMessageDialog(null, "correcto continuemos");
 
        }
 
        if (pregvaca == JOptionPane.OK_CANCEL_OPTION) {
 
            JOptionPane.showMessageDialog(null, "USUARIO NO RESPONDE");
 
        }
 
        String presentacion = JOptionPane.showInputDialog(null, "CUAL ES LA EVALUACION DE " + vendedor + " 0,10,20 ");
 
        JOptionPane.showInputDialog(null, "NOMBRE DEL SUPERVISOR :" + nombre);
 
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + vendedor);
 
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :" + presentacion);
 
        JOptionPane.showInputDialog(null, "NOMBRE DEL VENDEDOR :");
 
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

JOptionPane if

Publicado por kener (8 intervenciones) el 14/03/2020 00:12:25
si me funciono gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar