How to Read User Input with the JOptionPane Class in Java


In this article, we show how to read user input with the JOptionPane class in Java.

You're probably more familiar with how to read user input with the Scanner class.

Now we will show how to read user input with the JOptionPane Class with Java.

So below we show the full Java code for reading a string that a user inputs.

So this is the code shown above that allows a user to enter input using the JOptionPane class.

We'll now go through the code line by line so that you can understand each line.

In the first line, we import the JOptionPane class. The JOptionPane class is a part of the javax.swing package, so you need to add this import javax.swing.JOption Pane statement to the beginning of any proram that uses this class.

In the next statement, we create a public class called UserInput.

The next line declares the main function(), which every Java program must have.

We then create a string called userenters. This string will later hold the string that the user enters.

We set the userenters string equal to JOptionPane.showInputDialog("What is your name?"); This statement uses the showInputDialog method in the JOptionPane class to show an input dialog. The parameters that we put in this showInputDialog method is what is shown in the dialog box that pops up for the user. In this case, we ask the user for his or her name.

At this point, the user types in his or her name.

We then output that the name that the user entered.

And this ends our program.

Very simple.

Accepting a string as the parameter into the JOptionPane class is actual the simplest case of a parameter. '

If you're accepting another type of parameter such as an integer or a double, then you need to use parse methods to convert the string that is returned by this method into the appropriate primitive type.

As an example, we will show below how to read an integer that a user enters using the JOptionPane class.

Reading an Input Other than a String Using the JOptionPane Class

So we will show how to read any value other than a string value, which is what the JOptionPane class returns.

We will show how to parse this input into the appropriate type wanted.

Below we show how to read an integer input with the JOptionPane class.

So this is code shown above to read an integer input.

Everything is the same except we use the Integer wrapper class and the parseInt() function to parse the string that the user has entered. We place this value into the int i variable that we cdreated.

We then output the number that the user has entered.

Why this is important and why this is necessary is because anything that we enter into the JOptionPane dialog box is seen as a string. Therefore, unless you want it to be a string, then you have to parse into what you want. For example, say we are performing a mathematical calculation and we wanted a user to enter an integer so that we could perform some mathematical operation. If we didn't parse the string into integer type, even if you input an integer such as 2 or 5 or whatever, it would see as a string. Therefore, it wouldn't be able to perform the mathematical operation, since it sees it as a string. Therefore, when we parse it into an integer, it actually sees it as an integer and can perform the operation.

So this is a powerful concept and one that is important to be understood when getting user input other than a string.

Related Resources

How to Read Input with the Scanner Class in Java

HTML Comment Box is loading comments...