Since negative numbers do not have a real square root, the Schleife would be unable to compute it und would run forever.
There are several ways in which defective user input could be handled. One way is to check the N that the user entered. If it is OK, execute the Schleife. Otherwise, write an error message to the screen und end the Programm. Here is the revised Programm, with some blanks:
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.parseDouble( NChars );
_____________________________________
{
while ( Math.abs( N/(guess*guess) - 1.0 ) > smallValue )
{
guess = N/(2*guess) + guess/2 ; // calculate a new value for the guess
}
System.out.println("The square root of " + N + " is " + guess ) ;
}
________
_____________________________
}
}