CS 101 Homework

``````import java.text.*;
import java.io.*;

public class RootFinder {

public static void main(String[] args) {

Number input = null;

try {
NumberFormat nf = NumberFormat.getInstance();
while (true) {
System.out.println("Enter a number (-1 to quit): ");
try {
input = nf.parse(s);
}
catch (ParseException e) {
System.out.println(s + " is not a number I understand.");
continue;
}
double d = input.doubleValue();
if (d < 0) break;
double root = Math.sqrt(d);
System.out.println("The square root of " + s + " is " + root);
}
}
catch (IOException e) {
System.err.println(e);
}

}

}
```
```

Output:
```% java RootFinder
Enter a number (-1 to quit):
87
The square root of 87 is 9.327379053088816
Enter a number (-1 to quit):
65.4
The square root of 65.4 is 8.087026647662292
Enter a number (-1 to quit):
3.151592
The square root of 3.151592 is 1.7752723734683644
Enter a number (-1 to quit):
2,345,678
The square root of 2,345,678 is 1531.5606419596973
Enter a number (-1 to quit):
2.998E+8
The square root of 2.998E+8 is 1.7314733610425546
Enter a number (-1 to quit):
299800000
The square root of 299800000 is 17314.733610425545
Enter a number (-1 to quit):
0.0
The square root of 0.0 is 0.0
Enter a number (-1 to quit):
four
four is not a number I understand.
Enter a number (-1 to quit):
4
The square root of 4 is 2.0
Enter a number (-1 to quit):
Enter a number (-1 to quit):
(12)
(12) is not a number I understand.
-1

```