Next: Writing of numbers of
Up: Unit 04
Previous: Automatic boxing and unboxing
To read a number of type int from an input channel, we use:
- a method to read a string from an input channel (e.g.,
showInputDialog() of the class JOptionPane)
- 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: Writing of numbers of
Up: Unit 04
Previous: Automatic boxing and unboxing