• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking square root of a number multiple times using loops  RSS feed

 
Clayton Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone, I've only been self-teaching Java for a few weeks, so please bear with me. I'm trying to write a program that takes a number (that's greater than 10) from the console and take the square root of that number until it obtains a result that is LESS than 1.01. I'm also trying to count how many times the square root was taken when than less than 1.01 value was obtained. Part of the program works, as it will only accept valid input. I've tried various ideas, this being the last:


Can someone please point me in the right direction? Am I on the right track, or am I just way off base? The first part of the program (initial user input) works, but that's as far as it goes. The program won't even take the first square root and print that.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch and to Java community!!!

i do not have any Java compiler with me currently but just looking into it code, seem like code is going in infinite loop.
Reason - below mentioned line inside while condition on Line 28

You are calling sqrt on "number" variable every time in loop. so effectively , squareRoot variable remains same every time and value do not reduce to/become less than 1.01. so Control do not come out of while loop.
That line should be

So that code snippet will be

Now squareRoot variable value shall be updated everytime and eventually control shall come out of while loop.
Try this change and let us know if your problem is resolved.
~abhay
 
Clayton Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhay,

Thanks for the welcome! I like your suggestion, but the problem remains. When I enter a number, nothing at all happens. It's like it does not take the square root of the number the first time and never hits the if/else statement. Nothing at all prints out.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding extra print statements to show the flow through the code. If that doesn't help, you'll need to post your new code
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clayton Williams wrote: . . .
. . .
Don't put such code in the method you are using. That should go into a method all of its own, preferably in a utility class. For a utility class:
  • All members are static.
  • You never need an object so you prevent instantiation as shown here: private constructor, never called.
  • Call your method on the class name like this: int i = KeyboardInputs.getInt(); etc.
  • You will have to work out what the methods will look like:- Hint: The code will look very like what you wrote yourself.

    You appear to be asking the program to print the square root before you calculate it. You appear to be calling nextDouble twice. You are also incrementing count in two different place; that looks like awkward flow of control.
     
    Abhay Agarwal
    Ranch Hand
    Posts: 1376
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Clayton

    You have mentioned that The first part of the program (initial user input) works fine for you.

    As Stuart mentioned, please add more System.out.println line to check whether the number (which you are entering in command prompt) is getting assigned properly to 'number' variable in your java code.

    ~ abhay
     
    Robert D. Smith
    Ranch Hand
    Posts: 221
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was kind of bored this morning, waiting on my granddaughters to get ready for school, so I ran your code.

    The additional println suggestions is always a good idea.

    I reduced your (math) code to a half dozen lines.

    1. do the initial squareRoot calculation
    2. initialize Count to 1
    3. a simple while squareRoot > 1 loop until you get the result you are looking for.

    Oh, and as suggested, use the code blocks for your code. so much easier to read.


    Edit: minor operator error typo fixed.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!