Page 132 - Introduction to Programming with Java: A Problem Solving Approach
P. 132

                98 Chapter 3 Java Basics
The purpose of the input dialog is to read a user-entered value and store it in a variable. To read text
values, call showInputDialog like this:
<string-variable> = JOptionPane.showInputDialog(<prompt-message>);
To read in a number, you need to call showInputDialog and then convert the read-in string to a number. More specifically, to read an int value, do this:
<int-variable> = Integer.parseInt(JOptionPane.showInputDialog)(<prompt-message>); And to read a double value, do this:
<double-variable> = Double.parseDouble(JOptionPane.showInputDialog (<prompt-message>));
Now look at Figure 3.19. It shows how to use these new statements to produce Figure 3.18’s displays.
 /*******************************************************************
*
PrintPOGUI.java
Dean & Dean
This program calculates and prints a purchase order report.
*******************************************************************/
           Apago PDF Enhancer
import javax.swing.JOptionPane;
*
*
*
public class PrintPOGUI
{
}
// end class PrintPOGUI
public static void main(String[] args)
{
}
itemName = JOptionPane.showInputDialog("Name of purchase item:");
price = Double.parseDouble(
JOptionPane.showInputDialog("Price of one item:"));
qty = Integer.parseInt(
JOptionPane.showInputDialog("Quantity:"));
JOptionPane.showMessageDialog(null,
"PURCHASE ORDER:\n\n" +
"Item: " + itemName + "\nQuantity: " + qty +
"\nTotal price: $" + price * qty);
// end main
String itemName; // name of purchase item
double price;
int qty;
// price of purchase item
// number of items purchased
Figure 3.19 PrintPOGUI program

























































   130   131   132   133   134