0% found this document useful (0 votes)
146 views1 page

Using JOptionPane

This document discusses using the JOptionPane methods for input and output in Java programs. It explains that the JOptionPane class must be imported and then demonstrates how to use JOptionPane.showInputDialog to get string input from users and JOptionPane.showMessageDialog to output a message to users. It also shows how to convert string input to integer or double values using parse methods.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views1 page

Using JOptionPane

This document discusses using the JOptionPane methods for input and output in Java programs. It explains that the JOptionPane class must be imported and then demonstrates how to use JOptionPane.showInputDialog to get string input from users and JOptionPane.showMessageDialog to output a message to users. It also shows how to convert string input to integer or double values using parse methods.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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