CSC 108H

How to read a double value from input

Here are three ways to read a double value from input. All methods assume that you've already set up the input stream "in" in the usual way.

First method

	String line = in.readLine();
	double d = new Double(line).doubleValue();

Second method

	String line = in.readLine();
	double d = Double.valueOf(line).doubleValue();

Third method

This method is new, but better than both the others. If you're using Java version 1.2 or later, as you are when you work at CDF-PC, then you can do double input just like int input:

	String line = in.readLine();
	double d = Double.parseDouble(line);