8

I am trying to make a JOptionPane get an input and assign it to an int but I am getting some problems with the variable types.

I am trying something like this:

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

But I am getting:

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

Which sounds logical yet, I cannot think of another way to make this happen.

starball
  • 20,030
  • 7
  • 43
  • 238
kxk
  • 576
  • 2
  • 11
  • 30

6 Answers6

9

Simply use:

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

You cannot cast a String to an int, but you can convert it using Integer.parseInt(string).

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • Hmm... it seems I need to also add int ans = Integer.parseInt( JOptionPane.showInputDialog(frame, "Text", JOptionPane.INFORMATION_MESSAGE, null, null, "[sample text to help input]").toString()); – kxk Jun 25 '10 at 19:52
  • @devil If you use the correct form of `showinputdialog` you won't need to do that. But, you are correct in some cases. – jjnguy Jun 25 '10 at 19:55
  • I see I see. If I may ask one more thing, is there a way, using a loop maybe, to check if the input given actually is an integer or not? Sth like: ... do { ans = JOptionPane.showInputDialog(...) } until ans = integer ? – kxk Jun 25 '10 at 19:58
5

This because the input that the user inserts into the JOptionPane is a String and it is stored and returned as a String.

Java cannot convert between strings and number by itself, you have to use specific functions, just use:

int ans = Integer.parseInt(JOptionPane.showInputDialog(...))
Jack
  • 131,802
  • 30
  • 241
  • 343
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 ");
}
}
ricca
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 14 '21 at 05:43
0

Please note that Integer.parseInt throws an NumberFormatException if the passed string doesn't contain a parsable string.

Davy Meers
  • 1,758
  • 14
  • 8
0
// 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);
    }
}
marcospereira
  • 12,045
  • 3
  • 46
  • 52
MyStack
  • 1
  • 2
0
String String_firstNumber = JOptionPane.showInputDialog("Input  Semisecond");
int Int_firstNumber = Integer.parseInt(firstNumber);

Now your Int_firstnumber contains integer value of String_fristNumber.

hope it helped