You are on page 1of 1

//Age.

java /** * This program pops up * an option pane that asks * the user about his or her age * and returns the appropriate * reply. * * @author Charvi Shah */ import javax.swing.*; public class Age { public static void main(String [] args) { String ageText = JOptionPane.showInputDialog(null, "What's your age, cowboy?"); try { // converts the input string to an integer. int age = Integer.parseInt(ageText); if(age < 40) // displays message on screen. JOptionPane.showMessageDialog(null, "You are still young bud dy!!"); else JOptionPane.showMessageDialog(null, "You are an oldie!!"); } catch(NumberFormatException e) { // displays message if user inputs a string instead of a number JOptionPane.showMessageDialog(null, "Enter correct age."); } } }

You might also like