[英]How can I convert this JOptionPane result to integer value?
How can I make this JOptionPane.showInputDialog
return an integer value instead of a string value?如何使此
JOptionPane.showInputDialog
返回 integer 值而不是字符串值?
The values of the chains are in Spanish sorry in advance链条的值是西班牙语 提前抱歉
I put Integer.parseInt()
and it didn't work for me*我放
Integer.parseInt()
但它对我不起作用*
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]));
A good approach would be to try and understand the method you're using by checking the Javadocs一个好的方法是通过检查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
.showInputDialog()
返回类型Object
。 That return value is what you're passing to Integer.parseInt(String)
.该返回值就是您传递给
Integer.parseInt(String)
的值。 Again, this will not compile, since parseInt
expects a String
.同样,这不会编译,因为
parseInt
需要一个String
。 Object
does not automatically cast to String
. Object
不会自动转换为String
。 parseInt
is also in the javadocs, or your IDE will tell you about this error. parseInt
也在 javadocs 中,或者你的 IDE 会告诉你这个错误。
showInputDialog
returns the full String
that the user selected from the dropdown.showInputDialog
返回用户从下拉列表中选择的完整String
。 These come from your Options
array.Options
数组。 None of those can be directly translated to int
: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.所以你现在可以看到为什么
Integer.parseInt()
不起作用。
TLDR: Your code doesn't compile. TLDR:您的代码无法编译。 Refer to the Javadocs.
请参阅 Javadocs。 Simplify your problem by taking smaller steps.
通过采取更小的步骤来简化您的问题。
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在此之后,我使用了 java.util.Arrays 库,我采用 asList () 方法返回由指定数组支持的固定大小的列表,在这种情况下,它是选项数组,然后我使用 .indexOF () 方法根据选择返回 integer 值并将其分配给决策变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.