import javax.swing.JOptionPane; import java.util.Scanner; /** * This class demonstrates how we can build a program that can run without * BlueJ, yet still use input and output. It uses JOptionPane dialog boxes * to get input and display output. * * @author Brad Richards * @version 1.0 */ public class Cats { public static void main(String[] args) { // The argument is the text that will appear in the dialog box. // Whatever the user types is returned as a String. String name = JOptionPane.showInputDialog("What's your name?"); // Pop up a message dialog box with the response JOptionPane.showMessageDialog(null, "Hi "+name+", it's nice to meet you."); // We can get numeric inputs by first reading the user's input // as a String and then using a Scanner to grab an int from // the String! String catNumString = JOptionPane.showInputDialog("How many cats do you have?"); Scanner s = new Scanner(catNumString); int numCats = s.nextInt(); // Give them our opinion on the number of cats JOptionPane.showMessageDialog(null, "Anything more than "+numCats/2+" is too many!"); } }