I keep recieveing code errors and I'm not sure why

Can anyone let me know what I'm doing wrong

Page 1 of 1

8 Replies - 694 Views - Last Post: 11 October 2009 - 12:05 PM Rate Topic: -----

#1 FloridaGirlsRule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 09

I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 08:28 AM

I keep recieving a error that states:
SquareRootCalculator.java:30: ')' expected
if (nextguess = (lastGuess + (num / lastGuess)) / 2;
^
SquareRootCalculator.java:33: 'else' without 'if'
else if ((nextGuess - lastGuess) < 0.0001)
^
SquareRootCalculator.java:37: '(' expected
else if
^
SquareRootCalculator.java:38: ')' expected
lastGuess = nextGuess;
^
4 errors

Tool completed with exit code 1

I've tried to fix this many times and keep getting an error. I've tried asking my teacher and he gives me attitude and then won't ans the question. Can someone please help me on what my problem is? Thanks so much my code is below:


import java.util.Scanner;

public class SquareRootCalculator
{

public static void main (String [] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");
int num = input.nextInt();

double result = calculateSquareRoot(num);

System.out.print("The Square root is: " + Math.sqrt(num));




}
public static double calculateSquareRoot(double num)
{

double lastGuess = 1.0;
double nextGuess = 0.0;

while (true)
{

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

else if ((nextGuess - lastGuess) < 0.0001)
break;


else if
lastGuess = nextGuess;

}


return nextGuess;


}

}

Is This A Good Question/Topic? 0
  • +

Replies To: I keep recieveing code errors and I'm not sure why

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 08:43 AM

:code:

You have problems with 2 of your conditional statements. First:
if (nextguess = (lastGuess + (num / lastGuess)) / 2;


Remember that conditionals never end in semi-colons. They end in parentheses. Also, you need a comparison (==) operator, not an assignment operator (=). Let's redo it:
if (nextguess == (lastGuess + (num / lastGuess)/2)




Next with:
else if
lastGuess = nextGuess;



You are missing parentheses entirely. Try:
else if(lastGuess == nextGuess){
   ..do something..
}


Was This Post Helpful? 0
  • +
  • -

#3 Overachiever  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 36
  • Joined: 09-September 09

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 08:44 AM

Please put your code between code tags when you post it.

if (nextguess = (lastGuess + (num / lastGuess)) / 2;
This line is missing a ) after the 2.
Also, use two ='s in if statements.
Plus you forgot to capitalize the g in nextguess. Your variable was nextGuess.

else if ((nextGuess - lastGuess) < 0.0001)
You don't need the else there.

else if
  lastGuess = nextGuess;

Don't need the if there, unless you're gonna put some statement for it to check in parentheses.
Was This Post Helpful? 0
  • +
  • -

#4 Rykker  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 27
  • Joined: 03-October 09

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 08:44 AM

View PostFloridaGirlsRule, on 11 Oct, 2009 - 07:28 AM, said:

I keep recieving a error that states:
SquareRootCalculator.java:30: ')' expected
if (nextguess = (lastGuess + (num / lastGuess)) / 2;
^
SquareRootCalculator.java:33: 'else' without 'if'
else if ((nextGuess - lastGuess) < 0.0001)
^
SquareRootCalculator.java:37: '(' expected
else if
^
SquareRootCalculator.java:38: ')' expected
lastGuess = nextGuess;
^
4 errors

Tool completed with exit code 1

I've tried to fix this many times and keep getting an error. I've tried asking my teacher and he gives me attitude and then won't ans the question. Can someone please help me on what my problem is? Thanks so much my code is below:


import java.util.Scanner;

public class SquareRootCalculator
{

public static void main (String [] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");
int num = input.nextInt();

double result = calculateSquareRoot(num);

System.out.print("The Square root is: " + Math.sqrt(num));




}
public static double calculateSquareRoot(double num)
{

double lastGuess = 1.0;
double nextGuess = 0.0;

while (true)
{

if (nextGuess == (lastGuess + (num / lastGuess)) / 2)
{
System.out.println("Yes, the square is" + num);
}
else
{
if ((nextGuess - lastGuess) < 0.0001)
{}
}
break;


else
{
if(lastGuess == nextGuess)
{}
}

}


return nextGuess;


}

}

u have written a wrong if statement......just check it out......sometime when you are using more if statement then u must be carefull with their brackets......ok :^: :) B)

This post has been edited by Rykker: 11 October 2009 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#5 FloridaGirlsRule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 09

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 10:15 AM

I've tried the request everyone ask and I'm still getting a error message. I've changed the code to:

 import java.util.Scanner;

public class SquareRootCalculator
{

	public static void main (String [] args)
	{
		Scanner input = new Scanner(System.in);

		System.out.print("Enter a number:  ");
		int num = input.nextInt();

		double result = calculateSquareRoot(num);

		System.out.print("The Square root is:  " + Math.sqrt(num));




}
		public static double calculateSquareRoot(double num)
		{

		double lastGuess = 1.0;
		double nextGuess = 0.0;

	while (true)
	{
	if (nextGuess == (lastGuess + (num / lastGuess) / 2)
	System.out.println("Yes, the square is" + num);
}
	if ((nextGuess - lastGuess) < 0.0001)
		break;


	if (lastGuess = nextGuess);





	return nextGuess;
}

	}

}


the error code says:
SquareRootCalculator.java:29: ')' expected
if (nextGuess == (lastGuess + (num / lastGuess) / 2)
^
SquareRootCalculator.java:47: class, interface, or enum expected
}

I don't see anything else wrong and I've check by book like 50 times. This is the main error I get everytime I make a change.
Was This Post Helpful? 0
  • +
  • -

#6 FloridaGirlsRule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 09

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 11:43 AM

So in my previous post I tried to solve my problem with helpful request but I'm still getting error messages:
SquareRootCalculator.java:29: ')' expected
if (nextGuess == (lastGuess + (num / lastGuess) / 2)
^
SquareRootCalculator.java:47: class, interface, or enum expected
}
^
It is the same error message I've been getting before asking for help when I did make changes. Can anyone help? I'm so fustrated.

import java.util.Scanner;

public class SquareRootCalculator
{

	public static void main (String [] args)
	{
		Scanner input = new Scanner(System.in);

		System.out.print("Enter a number:  ");
		int num = input.nextInt();

		double result = calculateSquareRoot(num);

		System.out.print("The Square root is:  " + Math.sqrt(num));




}
		public static double calculateSquareRoot(double num)
		{

		double lastGuess = 1.0;
		double nextGuess = 0.0;

	while (true)
	{
	if (nextGuess == (lastGuess + (num / lastGuess) / 2)
	System.out.println("Yes, the square is" + num);
}
	if ((nextGuess - lastGuess) < 0.0001)
		break;


	if (lastGuess = nextGuess);





	return nextGuess;
}

	}

}
 

Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 11:48 AM

You need another parentheses to close your if statement. Right now, you've just closed the mathematical part on the right side of the ==

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

// add another ")"


:)

This post has been edited by Locke: 11 October 2009 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 11:52 AM

This error:
SquareRootCalculator.java:29: ')' expected
if (nextGuess == (lastGuess + (num / lastGuess) / 2)

Means that you are missing an end parenthesis at the given if statement.

The second error means that you have too many curly braces. So let's clean up your code and find that brace:
import java.util.Scanner;
public class SquareRootCalculator{

	public static void main (String [] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a number:  ");
		int num = input.nextInt();
		double result = calculateSquareRoot(num);

		System.out.print("The Square root is:  " + Math.sqrt(num));
	  }//end main
	  
  public static double calculateSquareRoot(double num){
		double lastGuess = 1.0;
		double nextGuess = 0.0;
		while (true){
			 if (nextGuess == (lastGuess + (num / lastGuess) / 2)
					 System.out.println("Yes, the square is" + num);
		 }//end while
	   if ((nextGuess - lastGuess) < 0.0001)
		  break; //does nothing 
  
	   if (lastGuess = nextGuess);
		   return nextGuess;
	 }//end method

}//end class

}//extraneous brace


Was This Post Helpful? 1
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: I keep recieveing code errors and I'm not sure why

Posted 11 October 2009 - 12:05 PM

your calculateSquareRoot() method will get you in an infinite loop btw, include the
if ((nextGuess - lastGuess) < 0.0001)
		break;



inside the while loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1