A good approach would be to try and understand the method you're using by checking the Javadocs
- First thing that I noticed, is your code will not even compile because you have too many parameters.
JOptionPane.showInputDialog(
null, // parent
"Digite la funcion que desea usar: ", // message
"Busetas", // title
JOptionPane.DEFAULT_OPTION, // ???? What's this?
JOptionPane.QUESTION_MESSAGE, // message type
null, // icon
Options, // selection values
Options[0]); // initial selection
- Next, according to the Javadocs,
showInputDialog()
returns type Object
.
That return value is what you're passing to Integer.parseInt(String)
. Again, this will not compile, since parseInt
expects a String
. Object
does not automatically cast to String
. parseInt
is also in the javadocs, or your IDE will tell you about this error.
- If you make your code work step-by-step to try and understand what is happening, you'll see that
showInputDialog
returns the full String
that the user selected from the dropdown. These come from your Options
array. None of those can be directly translated to int
:
// Use small first letter in variable names. This is java convention and will help readers of your code be less confused.
String[] options = {
"Mostrar la lista de ganancias de cada una de las busetas\n",
"Mostrar la buseta que más dinero gano esa semana\n",
"Mostrar la buseta que menos dinero gano esa semana\n",
"Mostrar el dia que mas gana cada una de las busetas\n",
"Aumentar las ventas que estan por debajo del promedio un 20%"
};
Object selection = JOptionPane.showInputDialog(
null,
"Digite la funcion que desea usar: ",
"Busetas",
// JOptionPane.DEFAULT_OPTION, // Removed this to make it work
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
System.out.println(selection); // This prints for example: Mostrar la lista de ganancias de cada una de las busetas
// Therefore below line will be like Integer.parseInt("Mostrar la lista...");
int decision = Integer.parseInt(selection);
So you can see now why Integer.parseInt()
did not work.
- My suggestion is look up how to find the index of a String within a String array: How to find index of STRING array in Java from a given value?
TLDR: Your code doesn't compile. Refer to the Javadocs. Simplify your problem by taking smaller steps.