Incompatible types Java error

0

I am doing a program where the user will give his number and countable and his name, however he does not run and an error appears saying: 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);

   }
}
    
asked by Stephanie B Bautista 27.01.2017 в 02:09
source

2 answers

3

One of your mistakes is here:

auxnum=Integer.parseInt(auxnum=JoptionPane.showInputDialog("Ingresa el numero contable"));

It should be just like that

    auxnum=Integer.parseInt(JoptionPane.showInputDialog("Ingresa el numero contable"));

The other mistake is here:

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");

It should be like this:

opcionmenu=Integer.parseInt(JOptionPane.showInputDialog("Ingresa la opcion deseada: \n 1.Captura \n 2.Consulta \n 3.Cambios \n 4.Cancelaciones \n 5.Reinstalacion \n 6.Salida") );

Those are the errors that I have identified that would have to do with the error that you indicate that your question.

    
answered by 27.01.2017 / 02:53
source
1

The error in your code is in this line:

opcionmenu = JOptionPane.showInputDialog(...);

It basically occurs because opcionmenu is a variable of type int , which expects you to store an integer number within it, however the function showInputDialog(...) of object JOptionPane returns a text string or string instead of a int , that is, whatever you have written in the input of the pop-up window, regardless of whether what you wrote has been a number.

To solve this you should only try to parse the string to int :

string input = JOptionPane.showInputDialog(...);
int opcionmenu = Integer.parseInt(input);

This code will work as long as what you have written is a whole number. But it will fail otherwise. To avoid this, you should only handle the exception that occurs in this case and prevent the application from breaking:

string input = JOptionPane.showInputDialog(...);

try {
  int opcionmenu = Integer.parseInt(input);
} catch(NumberFormatException e) {
  // Indicar al usuario que debe ingresar un número entero...
}
    
answered by 27.01.2017 в 02:53