• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Babylonian Square Root

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Enter your guess: 100
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
   
     
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic