Java Notes

Console vs Dialog I/O

Dialog box I/O is useful. Normal programs use a Graphical User Interface (GUI) that creates windows, etc. Because writing a full GUI interface requires understanding a few things first, introductory examples are often limited to using only dialog boxes for I/O.

Console I/O is less useful. Console I/O was the only kind of I/O that was built into programming langauges from the beginning of programming until recently. Textbooks were written to use it, and instructors learned it. It no longer makes much sense to use it, but it will take a while before most textbook authors change.

Brief summary to aid in converting console I/O to dialog boxes

Comments
Console The problem with learning console I/O is that it's almost totally useless in a real GUI program. The Scanner class is only available in Java 5 and beyond, and doing standard console input without Scanner is not a pretty picture for a beginner. Scanner will be useful when you learn text file input, but that topic is often taught at the end of the second course.
Dialog It's useful to learn to use dialog boxes because they're part of the GUI system, and are something that you will use in real GUI programs.
Imports
Console import java.util.*; // For Scanner class.
Dialog import javax.swing.*; // For JOptionPane class.
Initialization
Console Scanner input = new Scanner(System.in);
Dialog None required.
Output
Console System.out.println("Display this");
Dialog JOptionPane.showMessageDialog(null, "Display this");
Line of text input
Console String name;
System.out.print("Enter your name: ");
name = input.nextLine();
Dialog String name;
name = JOptionPane.showInputDialog(null, "Enter your name");
Integer input
Console System.out.print("Enter your age: ");
int age = input.nextInt();
Dialog String aStr = JOptionPane.showInputDialog(null, "Enter your age");
int age = Integer.parseInt(aStr);
Or these could be combined on one line, eliminating the temporary variable:
int age = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age"));

Example program written two ways

Here's a simple program to average three ints, written once with JOptionPane and again with Scanner.

JOptionPane version reads and writes using dialog boxes. Because the showInputDialog method always returns a String, it has to be converted to a numeric type with, eg, Integer.parseInt().

Scanner was introduced in Java 5 (JDK 1.5) and is not available in older versions of Java. It can be used for input from the console and files, but not for GUI input. It has a large number of useful methods.

Average3 using JOptionPane

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
// File:   average3/Average3JOptionPane.java
// Description: Average three ints.  Use JOptionPane.
// Author: Fred Swartz
// Date:   2005-08-30

import javax.swing.*;

public class Average3JOptionPane {

    public static void main(String[] args) {

        //... Declare local variables;
        int a, b, c;  // No meaningful names are possible.
        int average;
        String temp;  // Temporary storage for JOptionPane input.

        //... Read three numbers from dialog boxes.
        temp = JOptionPane.showInputDialog(null, "First number");
        a = Integer.parseInt(temp);  // Convert String to int.
        temp = JOptionPane.showInputDialog(null, "Second number");
        b = Integer.parseInt(temp);
        temp = JOptionPane.showInputDialog(null, "Third number");
        c = Integer.parseInt(temp);

        //... Compute their average.
        average = (a + b + c) / 3;

        //... Display their average in a dialog box.
        JOptionPane.showMessageDialog(null, "Average is " + average);
    }
}

Average3 using Scanner and println

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
// File:   average3/Average3Scanner.java
// Description: Average three ints.  Use Scanner.
// Author: Fred Swartz
// Date:   2005-08-30

// Note: Scanner was added in Java 5 (JDK 1.5).

import java.util.Scanner;

public class Average3Scanner {

    public static void main(String[] args) {

        //... Declare local variables;
        int a, b, c;  // No meaningful names are possible.
        int average;

        //... Initialize Scanner to read from console.
        Scanner input = new Scanner(System.in);

        //... Read three numbers from the console.
        System.out.print("Enter first number : ");
        a = input.nextInt();
        System.out.print("Enter second number: ");
        b = input.nextInt();
        System.out.print("Enter last number  : ");
        c = input.nextInt();

        //... Compute their average.
        average = (a + b + c) / 3;

        //... Display their average on the console.
        System.out.println("Average is " + average);
    }
}