Reputation: 13
How can I make this JOptionPane.showInputDialog
return an integer value instead of a string value?
The values of the chains are in Spanish sorry in advance
I put Integer.parseInt()
and it didn't work for me*
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%"
};
int decision = Integer.parseInt(
JOptionPane.showInputDialog(
null,
"Digite la funcion que desea usar: ",
"Busetas",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
Options,
Options[0]));
Upvotes: 0
Views: 156
Reputation: 675
A good approach would be to try and understand the method you're using by checking the Javadocs
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
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.
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.
TLDR: Your code doesn't compile. Refer to the Javadocs. Simplify your problem by taking smaller steps.
Upvotes: 2
Reputation: 13
Thanks for the help. I attach the solution and explanation of the change I made in the code in case someone needs it
String[] options = {
"Mostrar la lista de ganancias de cada una de las busetas",
"Mostrar la buseta que más dinero gano esa semana",
"Mostrar la buseta que menos dinero gano esa semana",
"Mostrar el dia que mas gana cada una de las busetas",
"Aumentar las ventas que estan por debajo del promedio un 20%",
"Salir"};
Object selection = JOptionPane.showInputDialog(
null,
"Seleccione una de las opciones disponibles",
"Busetas",
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
decision = Arrays.asList(options).indexOf(selection);
I made use of the java.util.Arrays library after this, I take the asList () method that returns us a list of fixed size backed by the specified array, in this case, it is the options array then I use the .indexOF () method to return an integer value depending on the selection and assign it to the decision variable
Upvotes: 0