/* GUI_IO1.java CIS 160 David Klick 2015-09-02 Demonstrates simple GUI I/O. Note: Unexpected user input will crash this program. */ import javax.swing.JOptionPane; // needed for JOptionPane public class GUI_IO1 { public static void main(String[] args) { String name, strIn; int age; // Get name from user - easy since it is a String name = JOptionPane.showInputDialog("Please enter your name"); // JOptionPane can get input, but it will be a String. // Get age from user - must convert from String to int strIn = JOptionPane.showInputDialog("Please enter your age"); age = Integer.parseInt(strIn); // Display what was read in JOptionPane.showMessageDialog(null, "Your name is " + name); JOptionPane.showMessageDialog(null, "Your age is " + age); /* Please note that an object reference is required as the second argument to JOptionPane.showMessageDialog(). This is fine if you are displaying a String since Strings are objects, but the eight primitive data types (boolean, char, byte, short, int, long, float, double) are NOT objects. This means that you can NOT display them directly. In other words, this is NOT allowed: JOptionPane.showMessageDialog(null, age); If you do want to display just a number, use the following trick: JOptionPane.showMessageDialog(null, "" + age); The expression ["" + age] will have to be evaluated before sending the information to JOptionPane.showMessageDialog(). Java will see you are using "+" with a number to a String. It knows it can't add a number to a String, but it can turn the number into a String of characters and append it to the other String. Since the other String contains nothing, you have conveniently converted the number into a String. This String is then sent to JOptionPane.showMessageDialog(). */ } }