The revised program is given below
Checking the user input is done with an if-else structure:
import java.io.*;
class SquareRoot
{
public static void main( String[] args ) throws IOException
{
final double smallValue = 1.0E-14 ;
double N ; // the user enters N
double guess = 1.00 ; // the same first guess works for any N
// get the number from the user
String NChars;
BufferedReader stdin =
new BufferedReader( new InputStreamReader(System.in) );
System.out.println("Enter the number:");
NChars = stdin.readLine();
N = ( Double.valueOf( NChars ) ).doubleValue();
if ( N >= 0.0 )
{
while ( Math.abs( N/(guess*guess) - 1.0 ) > smallValue )
{
guess = N/(2*guess) + guess/2 ; // calculate a new guess
}
System.out.println("The square root of " + N + " is " + guess ) ;
}
else
System.out.println("Please enter a positive number");
}
}