【问题标题】:JOptionPane Input to intJOptionPane 输入到 int
【发布时间】:2011-03-08 10:45:10
【问题描述】:

我正在尝试让 JOptionPane 获取输入并将其分配给 int,但我在变量类型方面遇到了一些问题。

我正在尝试这样的事情:

Int ans = (Integer) JOptionPane.showInputDialog(frame,
            "Text",
            JOptionPane.INFORMATION_MESSAGE,
            null,
            null,
            "[sample text to help input]");

但我得到了:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot
be cast to java.lang.Integer

这听起来合乎逻辑,但我想不出另一种方法来实现这一点。

提前致谢

【问题讨论】:

    标签: java swing user-input int joptionpane


    【解决方案1】:
    import javax.swing.*;
    public class JOptionSample { 
        public static void main(String[] args) {
    
           String name = JOptionPane.showInputDialog("Enter First integer");
    
          String name2 = JOptionPane.showInputDialog("Enter second integer");
    
           JOptionPane.showMessageDialog(null, "The first inputted is 89 and the second 
        integers inputted is 45" );
    
        int number =Integer.parseInt(JOptionPane.showInputDialog(null, "89+45 = "));
    
        JOptionPane.showMessageDialog(null, "Question Message", "Title",
       JOptionPane.QUESTION_MESSAGE);
    
        int option = JOptionPane.showConfirmDialog(null, "Do you want to continue? ");
        JOptionPane.showMessageDialog(null, "Your choice is "+option);
    
        JOptionPane.showMessageDialog(null, " The sum of the two integers is : 134 ");
    }
    }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:
    String String_firstNumber = JOptionPane.showInputDialog("Input  Semisecond");
    int Int_firstNumber = Integer.parseInt(firstNumber);
    

    现在您的Int_firstnumber 包含String_fristNumber 的整数值。

    希望对你有帮助

    【讨论】:

      【解决方案3】:
      // sample code for addition using JOptionPane
      
      import javax.swing.JOptionPane;
      
      public class Addition {
      
          public static void main(String[] args) {
      
              String firstNumber = JOptionPane.showInputDialog("Input <First Integer>");
      
              String secondNumber = JOptionPane.showInputDialog("Input <Second Integer>");
      
              int num1 = Integer.parseInt(firstNumber);
              int num2 = Integer.parseInt(secondNumber);
              int sum = num1 + num2;
              JOptionPane.showMessageDialog(null, "Sum is" + sum, "Sum of two Integers", JOptionPane.PLAIN_MESSAGE);
          }
      }
      

      【讨论】:

        【解决方案4】:

        请注意,如果传递的字符串不包含可解析字符串,则 Integer.parseInt 会引发 NumberFormatException。

        【讨论】:

          【解决方案5】:

          这是因为用户插入到JOptionPane 中的输入是String,它被存储并返回为String

          Java本身不能在字符串和数字之间进行转换,你必须使用特定的函数,使用:

          int ans = Integer.parseInt(JOptionPane.showInputDialog(...))
          

          【讨论】:

            【解决方案6】:

            简单使用:

            int ans = Integer.parseInt( JOptionPane.showInputDialog(frame,
                    "Text",
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    null,
                    "[sample text to help input]"));
            

            您不能将String 转换为int,但可以使用Integer.parseInt(string) 进行转换。

            【讨论】:

            • 嗯...看来我还需要添加 int ans = Integer.parseInt( JOptionPane.showInputDialog(frame, "Text", JOptionPane.INFORMATION_MESSAGE, null, null, "[示例文本帮助输入]").toString());
            • @devil 如果您使用正确的showinputdialog 形式,则无需这样做。但是,在某些情况下你是对的。
            • 我明白了。如果我可以再问一件事,有没有办法使用循环来检查给定的输入是否实际上是整数?诸如此类: ... do { ans = JOptionPane.showInputDialog(...) } until ans = integer ?
            猜你喜欢
            • 1970-01-01
            • 2016-03-17
            • 1970-01-01
            • 1970-01-01
            • 2011-01-06
            • 2011-12-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多