I am making a program where the user will give his number and accountant and his name, however it does not run and an error appears saying error: incompatible types required: int found: String
What am I doing wrong in my code?
import javax.swing.JOptionPane;
public class Capturas{
public static void main(String[]args){
String[]nombre=new String[5];
String auxnombre;
int[]num=new int[5];
int opcionmenu=0;
int capture1;
int ejecutar=0;
int auxnum=0;
int casillas=0;
int x=0;
int error;
int otro=0;
int yaesta=0;
int renglon;
do
{
opcionmenu=JOptionPane.showInputDialog("Ingresa la opcion deseada: \n 1.Captura \n 2.Consulta \n 3.Cambios \n 4.Cancelaciones \n 5.Reinstalacion \n 6.Salida");
if(opcionmenu==1)
{
if(casillas<4)
{
yaesta=0;
auxnum=Integer.parseInt(auxnum=JoptionPane.showInputDialog("Ingresa el numero contable"));
for(x=1; x<=4; x++)
{
if(auxnum==num[x])
{
yaesta=1;
}
}
if(yaesta==1)
{
JOptionPane.showMessageDialog(null, "El numero ya esta registrado");
}
if(yaesta==0)
{
renglon=0;
for(x=1; x<=4; x++)
{
if(nombre[x]!=null)
{
renglon=renglon+1;
}
}
renglon=renglon+1;
num[renglon]=auxnum;
nombre[renglon]=auxnum;
nombre[renglon]=JOptionPane.showInputDialog("Ingresa el Nombre");
casillas=casillas+1;
}
}
}
}
while(opcion!=6);
}
}
One of your mistakes is here:
it should be just like this
The other error is here:
It should be like that:
Those are the errors that I have identified that would have to do with the error that you indicate in your question.
The error in your code is on this line:
Basically it occurs because
opcionmenu
it's a variable of typeint
, which expects you to store an integer inside it, however theshowInputDialog(...)
object functionJOptionPane
returns a string orstring
instead of aint
, i.e. whatever you typed into the input of the pop-up window, regardless of whether what you typed was a number.To solve it you just have to try to parse the
string
aint
:This code will work as long as what you have typed is an integer. But it will fail otherwise. To avoid this, you should only handle the exception that is thrown in this case and prevent the application from breaking: