JOptionPane Input to int
Asked Answered
M

6

7

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.

Mccullers answered 25/6, 2010 at 19:41 Comment(0)
E
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).

Erdrich answered 25/6, 2010 at 19:42 Comment(3)
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());Mccullers
@devil If you use the correct form of showinputdialog you won't need to do that. But, you are correct in some cases.Erdrich
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 ?Mccullers
F
4

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(...))
Fluctuation answered 25/6, 2010 at 19:43 Comment(0)
C
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 ");
}
}
Cristobalcristobalite answered 14/10, 2021 at 4:38 Comment(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.Justificatory
I
0

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

Ibadan answered 25/6, 2010 at 21:12 Comment(0)
N
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);
    }
}
Nudge answered 6/4, 2016 at 18:5 Comment(0)
B
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

Browse answered 1/1, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.