简体   繁体   English

如何将此 JOptionPane 结果转换为 integer 值?

[英]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来尝试了解您正在使用的方法

  1. 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
  1. Next, according to the Javadocs, showInputDialog() returns type Object .接下来,根据 Javadocs, 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 会告诉你这个错误。

  1. 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.如果您逐步使代码工作以尝试了解正在发生的事情,您将看到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()不起作用。

  1. 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?我的建议是查找如何在字符串数组中查找字符串的索引: How to find index of STRING array in Java from a given value?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM