java approximating square root using while loop

Approximating Square Root Won't Compile

Page 1 of 1

5 Replies - 6939 Views - Last Post: 22 February 2009 - 09:27 PM Rate Topic: -----

#1 moxygirl11  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 19-February 09

java approximating square root using while loop

Post icon  Posted 20 February 2009 - 11:39 AM

Here's the error output I'm getting while compiling:

if (nextGuess = lastGuess + (num / lastGuess) / 2
^

illegal start of type
else if (nextGuess - lastGuess) < 0

illegal start of expression
break;
^
3 errors

Tool completed with exit code 1

Here's my code:

 import javax.swing.JOptionPane;

 public class SquareRootCalculatorc
 {
	 // main method, starting point of execution
	 public static void main (String [] args)
	 {
		 // read input from the user
		 String numString = JOptionPane.showInputDialog("Enter number, for example 2.0;");
		 double num = Double.parseDouble(numString);

		 // store input in a variable
		double num = lastGuess;

		// call square root method here
		double result = calculateSquareRoot(number);

		// display square root of number entered by the user
		System.out.println("The square root of " + number + " is " + result);


		// compare your result withe the Math.sqrt() method

	 }


	 // sqaure root method
	 public static double calculateSquareRoot(double num)
	 {
		// this is the procedure shown in the programming exercise
		// initialize last guess to 1.0
		double lastGuess = 1.0;

		// initialize next guess to 0.0
		double nextGuess = 0.0;

		// loop for ever
		// for (;; )
		while (true)			// this is easier to understand
		{ System.out.print("\nEnter your number:");
		double num = input.nextDouble();

			// calculate next guess based on current value of last guess
			// use the formula shown in the exercise

		if (nextGuess = lastGuess + (num / lastGuess) / 2
		  System.out.println("Yes, the square root is" + result);

		else if (nextGuess - lastGuess) < 0

			// if difference between next and last guess is less than 0.0001

				// exit loop
				break;
		 }

			// otherwise


				// assign next guess to last guess


		// outside of loop, return next guess

	 }
  }	 



Any advice? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: java approximating square root using while loop

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: java approximating square root using while loop

Posted 20 February 2009 - 11:47 AM

for a start in statement
if (nextGuess = lastGuess + (num / lastGuess) / 2


put brackets in the correct place and use a == to test for equality, e.g.
		if (nextGuess == (lastGuess + (num / lastGuess) / 2))


This post has been edited by horace: 20 February 2009 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 moxygirl11  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 19-February 09

Re: java approximating square root using while loop

Posted 20 February 2009 - 11:57 AM

View Posthorace, on 20 Feb, 2009 - 10:47 AM, said:

for a start in statement
if (nextGuess = lastGuess + (num / lastGuess) / 2


put brackets in the correct place and use a == to test for equality, e.g.
		if (nextGuess == (lastGuess + (num / lastGuess) / 2))




Thanks, I'll try this. mg
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: java approximating square root using while loop

Posted 20 February 2009 - 10:49 PM

also move the less than sign in your if
else if (nextGuess - lastGuess < 0) 


Was This Post Helpful? 1
  • +
  • -

#5 moxygirl11  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 19-February 09

Re: java approximating square root using while loop

Posted 22 February 2009 - 09:24 PM

View Postmostyfriedman, on 20 Feb, 2009 - 09:49 PM, said:

also move the less than sign in your if
else if (nextGuess - lastGuess < 0) 



Thanks for the motivation. I'm going to work on it tomorrow morning now that I'm not working full time like alot of people I know, and am thinking I'm getting to like it now I discovered and calling methods is the key, just needed to get my mind more organized and really analyzing the textbook. School is a nice distraction from the economy at least. Thanks for offering to help, I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

Re: java approximating square root using while loop

Posted 22 February 2009 - 09:27 PM

The standard if statement structure is...

if (CONDITION)

The condition MUST go in parentheses. The entire thing. If you need more parentheses, use them...

if (someFlag && (number1 - number2 == 1))

// notice how the ENTIRE thing is in ( )


Hope this helps! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1