Java: Console vs Dialog I/O

Programs use a Graphical User Interface (GUI) that creates windows, etc. Because GUI programming requires understanding quite a few things first, introductory examples typically start by using simpler I/O. The two most frequent choices are System.out.println and JOptionPane for output, and Scanner and JOptionPane for input. Here is a brief guide for translating between them.

Comparing console I/O to dialog boxes I/O

CharacteristicConsoleDialog
Comments Console output is simple, but useless in a real GUI program. Altho console input is not possible in a GUI program, the Scanner class is useful for reading from files. Dialog input is a wordy and requires explicit for data conversion, but it works in a GUI program. Dialog output is common in GUI programs.
Imports import java.util.*; // Scanner import javax.swing.*; // JOptionPane
Initialization // Declare and init Scanner object.
Scanner input = new Scanner(System.in);
None.
Line of text input System.out.print("Enter your name: ");
String name;
name = input.nextLine();
String name;
name = JOptionPane.showInputDialog(null,
       "Enter your name");
Integer input System.out.print("Enter your age: ");
int age = input.nextInt();
String aStr = JOptionPane.showInputDialog(null,
       "Enter your age");
int age = Integer.parseInt(aStr);
Output System.out.println(result); JOptionPane.showMessageDialog(null, result);

Why JOptionPane or GUI programming is used in most examples

It's best to use a GUI program, but when you start to learn programming, something simpler may be appropriate. Because no normal program does console I/O, it's better to use something that is compatible with standard GUI programming - JOptionPane.

Here's a simple program to average three ints, written using two simple input styles: JOptionPane and Scanner.

JOptionPane
This reads and writes using dialog boxes. Because the showInputDialog method returns only a String, it has to be converted to a numeric type with, eg, Integer.parseInt().
Scanner
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.

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 
 32 
 33 
// File:   average3/Average3JOptionPane.java
// Description: Average three ints.  Use JOptionPane.
// Author: Fred Swartz
// Date:   2002-02-13

import javax.swing.JOptionPane;

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, "First number");
        b = Integer.parseInt(temp);
        temp = JOptionPane.showInputDialog(null, "First 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);
    }
}

Scanner

  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:   2002-02-13

// Note: Scanner was added to 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);
    }
}