Getting input from user using JoptionPane class


Getting input from userusing JoptionPane class

In this class we are going to larn JoptionPane class to get the interactive inputs from the keyboard or files.. JoptionPane class is defined in java.swing*.* package. As soon as we typeJOptionPane() the system prompts for a hint to java.swing*.* package we can ALT+Enter to include that package.

JoptionPane.showInputDialog(“”);the showInputDialog() method in JoptionPane class accepts string as argument. See the example below:

JOptionPane.showInputDialog(“Please enter your name”); This method returns only strings and not numbers. To input integers or float you need to input as string and then convert it to int (see example-2).

it returns string, so should be saved in string variable. Now, let us see how we can display these values. We will now use JOptionPane.showmessageBox();

JOptionPane.showMessageBox(null, message name+” “ + surname);

here the null argument as of now say display the dialog box in the middle of the screen and message is the actual vlues of varibles that is required to be displayed. 

Example to use showInputDialog() and showMessageDialog() to input strings and display messages below:

package com.Examples;

import javax.swing.*;
import java.util.Scanner;

public class readingInputs
{

public static void main(String[] args)

{

String name;
String surname;
name= JOptionPane.showInputDialog(“please enter you first name”);
surname=JOptionPane.showInputDialog(“Please enter your surname”);
JOptionPane.showMessageDialog(null, name+” ” +surname);

}

}

Output

On executing the program will display a InputDialogBox() that prompts name and surname in the text field. The showMessageDialog() will display the name and surname.

 

various parse methods:

showInputDialog() is used to input string, if however, data is required in other formats such as integer, byte, double etc, the string input from the showInputDialog() is to be converted to one the types using the parse methods as given below. More of such methods are displayed as we type in the IntelliJ IDE.

method
Purpose
Integer.parseInt(string s);
Converts string to integer
Integer.parseInt(string s, int radix)
Converts string to number in bese r
Integer.parseUnsignedInt(string);
Converts string to unsigned integer
Double.parseDouble(string);
Converts string to double
Integer.bitCount(int i)
Integer.compare(int x, int y)
Integer.getInteger(string nm, int val)

example-2:

To input integers using JoptionPane.showInputDialog(); making use of Integer.parseInt(string s)

package com.Examples;

import javax.swing.*;
import java.util.Scanner;

public class readingInputs
{

public static void main(String[] args)

{

int firstNumber;
int secondNumber;
//declare a String variable to get value using JOptionpane.showInputDialog();
String input;
input=JOptionPane.showInputDialog(“Please enter a number”); //will enter sstring instead
//convert string to integer
firstNumber=Integer.parseInt(input);
input=JOptionPane.showInputDialog(“Please input second number”);
//convert to integer
secondNumber=Integer.parseInt(input);
double average;
average=(firstNumber+secondNumber)/2.0;
JOptionPane.showMessageDialog(null, “average”+average);

}

}

error: Content is protected !!