next up previous
Next: Writing of numbers of Up: Unit 04 Previous: Primitive data types for

Reading of numbers of type double or float

To read a number of type double (or float) 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 parseDouble (respectively, parseFloat) of the class Double (respectively, Float) to obtain the number corresponding to the string read as a value of type double (respectively, float).

Example:

String s = JOptionPane.showInputDialog("Insert a number (e.g., 3.14)");
double i = Double.parseDouble(s);
or
double i = Double.parseDouble(
            JOptionPane.showInputDialog("Insert a number (e.g., 3.14)"));


next up previous
Next: Writing of numbers of Up: Unit 04 Previous: Primitive data types for