next up previous
Next: Example: initials of a Up: Unit 02 Previous: Example: initials of a

Keyboard input

In Java there are many ways to read strings from input. The simplest one is to use the predefined method showInputDialog, which is defined in the class JOptionPane, which in turn is part of the swing library. Using such a method, we can read input from the keyboard according to the following schema:

import javax.swing.JOptionPane;

public class KeyboardInput {
  public static void main (String[] args) {
    ...
    String inputString = JOptionPane.showInputDialog("Insert a string");
    ...
    System.out.println(inputString);
    ...
    System.exit(0);
  }
}


next up previous
Next: Example: initials of a Up: Unit 02 Previous: Example: initials of a