next up previous
Next: Writing of numbers of Up: Unit 04 Previous: Wrapper classes for the

Reading of numbers of type int

To read a number of type int from an input channel, we use:

  1. a method to read a string from an input channel (e.g., showInputDialog of the class JOptionPane)
  2. the static method parseInt of the class Integer to obtain the number corresponding to the string read as a value of type int.

Example:

String s = JOptionPane.showInputDialog("Insert an integer number");
int i = Integer.parseInt(s);
or
int i = Integer.parseInt(
          JOptionPane.showInputDialog("Insert an integer number"));

Note that, if parseInt is called on a string containing characters different from digits, an error occurs when the program is run.


next up previous
Next: Writing of numbers of Up: Unit 04 Previous: Wrapper classes for the