Win a copy of Head First Agile this week in the Agile forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Babylonian Square Root

Megan Long
Greenhorn
Posts: 6

So I wrote this code and I keep getting this error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at BabylonianSquareRootVersionB.main(BabylonianSquareRootVersionB.java:11)

It's in the line * String N = args[0];* but I can't figure out what's wrong with it. any help? Thank you so much.

This is what the final result is supposed to look like:

Enter the value for n: 10
n is 10.0
Final guess is 3.162277660168379
The estimation error is 4.440892098500626E-16

and these are the instructions:

The Babylonian algorithm to compute the square root of a positive number, n, is as follows:
a. Make a guess at the answer
b. Compute r= n/guess
c. Set guess = (guess + r)/2
d. Go back to step (b) for as many iterations as are necessary. The more you repeat steps (b) and (c), the closer
guess will become to the square root of n.

PART B: Write a second program (name it BabylonianSquareRootVersionB) that:
1. Prompts the user for values for n and for guess at runtime, and receives the userâ€™s responses using
Scanner. Store the values as doubles.
2. As with the previous program, execute steps (b) and (c) 10 times and display the value of n, the final value
of guess and the difference between guess and the actual square root of n

Otto Zeimer
Greenhorn
Posts: 16
Right, so the reason you get this exception is because when you run a command from a command line, you probably run it without arguments, such as:

and it should be like this:

that variable string is what you are trying to assign at line 11 to a variable N.
if you don't pass any argument when running the application, you get this exception. Moreover, if you pass 1 argument and not 2, then you get the same exception on line 12

Otto Zeimer
Greenhorn
Posts: 16
Folks here could certainly write the whole code for your requirements, but then your learning process would be severely interrupted.
Pointers:
- Please chose variable names from lowercase letter. (it will help reading the code)
- As for part B.2 I would suggest creating a loop.
- I would suggest closing the scanner created on line 17 with this line at the end of the code block:

Carey Brown
Bartender
Posts: 3022
46
Otto Zeimer wrote:- I would suggest closing the scanner created on line 17 with this line at the end of the code block:

You should always close a scanner object when you are done with it EXCEPT if it has been created with System.in.

Campbell Ritchie
Marshal
Posts: 55793
164
Otto Zeimer wrote:. . .
- I would suggest closing the scanner created on line 17 with this line at the end of the code block:

Nononononononono! Never close a Scanner pointing to System.in. If you do that, you close System.in and you can never reopen it.