Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Name

JOptionPane Methods Input/Output CS 1

We will be using the JOptionPane methods for input and output. You must import this class so include:

import javax.swing.JOptionPane;

or

import javax.swing.*;

with every program that uses either statement.

Input: We will use the JOptionPane.showInputDialog to get input from the user. The line that generated the below dialog was:

String firstName = JOptionPane.showInputDialog("Enter your first name please: ");

The information is received as a String. If you wish to convert that information to an integer or a double you need to add:

String sIntInfo = JOptionPane.showInputDialog("Enter an integer: "); int intNum = Integer.parseInt(sIntInfo);


or

String sDoubleInfo = JOptionPane.showInputDialog("Enter a double: "); double doubleNum = Double.parseDouble(sDoubleInfo);

Output: We will use the JOptionPane.showMessageDialog to output information to the user. The line that generated the below dialog was:

JOptionPane.showMessageDialog (null,"Nice to meet you " + firstName + ".");

You might also like