Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
baileyr12

Can't get Math.sqrt() to work..need help!! [HELP]

12 posts in this topic

Ok, so what I have to do is write a program that will take 3 inputted values, plug them into the quadratic formula, then print out the roots. But when I try to use "Math.sqrt();" keep getting this error

 

C:\Users\Bailey\Desktop>javac PRogram2.java
PRogram2.java:31: error: cannot find symbol
                double rt1 = ((b*-1)+(Math.sqrt(e)))/d;
                                          ^
  symbol:   method sqrt(double)
  location: class Math
PRogram2.java:32: error: cannot find symbol
                double rt2 = ((b*-1)-(Math.sqrt(e)))/d;
                                          ^
  symbol:   method sqrt(double)
  location: class Math
2 errors
 
 
Here is my code:

import  java.util.*;import java.util.Scanner;public class Program2{	public static void main (String args[])	{		double a;		double b;		double c;				Scanner scan = new Scanner(System.in);		System.out.println("\nHello, and welcome to Bailey quadratic root finder!");		System.out.println("Please enter a value for 'A'(Ax^2+Bx+C):\n");		a = scan.nextDouble();		System.out.println("Now, please enter a value for 'B'(Ax^2+Bx+C):\n");		b = scan.nextDouble();		System.out.println("Finally, enter a value for 'C'(Ax^2+Bx+C):\n");		c = scan.nextDouble();		System.out.printf("Great! This is what your equation should look like:\n%.1fx^2 + %.1fx + %.1f = ",a,b,c);				double d = 2*a;		double e = ((a*a)-(4*a*c))/d;		double rt1 = ((b*-1)+(Math.sqrt(e)))/d;		double rt2 = ((b*-1)-(Math.sqrt(e)))/d;		System.out.printf("\nThe equation %.1fx^2 + %.1fx + %.1f has the following roots:\n", a, b, c);		//System.out.printf("Root 1: %.1f\nRoot 2: %.1f", rt1, rt2);	}}

 

Thanks!!

Edited by baileyr12

Share this post


Link to post
Share on other sites

What version of java are you using? It ran fine for me using Java 8.

Edited by Encoded

Share this post


Link to post
Share on other sites

-b, adding the negative like that doesn't make the value of b negative. Java sees this as you are trying to subtract b from something, but there is nothing there for b to be subtracted from.

fixed by changing it to (b*-1)

Share this post


Link to post
Share on other sites

-b, adding the negative like that doesn't make the value of b negative. Java sees this as you are trying to subtract b from something, but there is nothing there for b to be subtracted from.

What version of java are you using? It ran fine for me using Java 8.

im running java 8 too...

Share this post


Link to post
Share on other sites

-b, adding the negative like that doesn't make the value of b negative. Java sees this as you are trying to subtract b from something, but there is nothing there for b to be subtracted from.

What version of java are you using? It ran fine for me using Java 8.

This is my errors now:

 

C:\Users\Bailey\Desktop>javac PRogram2.java
PRogram2.java:31: error: cannot find symbol
                double rt1 = ((b*-1)+(Math.sqrt(e)))/d;
                                          ^
  symbol:   method sqrt(double)
  location: class Math
PRogram2.java:32: error: cannot find symbol
                double rt2 = ((b*-1)-(Math.sqrt(e)))/d;
                                          ^
  symbol:   method sqrt(double)
  location: class Math
2 errors

Share this post


Link to post
Share on other sites

fixed by changing it to (b*-1)

You can change it back to the -b, I was thinking of something different. This appears to be a compiler problem.

 

Here's the output I got:

Hello, and welcome to Bailey quadratic root finder!Please enter a value for 'A'(Ax^2+Bx+C):1Now, please enter a value for 'B'(Ax^2+Bx+C):-2Finally, enter a value for 'C'(Ax^2+Bx+C):-2Great! This is what your equation should look like:1.0x^2 + -2.0x + -2.0 = The equation 1.0x^2 + -2.0x + -2.0 has the following roots:Root 1: 2.1Root 2: -0.1Process finished with exit code 0

Using this code:

 import java.util.Scanner;public class Test{    public static void main (String args[])    {        double a;        double b;        double c;        Scanner scan = new Scanner(System.in);        System.out.println("\nHello, and welcome to Bailey quadratic root finder!");        System.out.println("Please enter a value for 'A'(Ax^2+Bx+C):\n");        a = scan.nextDouble();        System.out.println("Now, please enter a value for 'B'(Ax^2+Bx+C):\n");        b = scan.nextDouble();        System.out.println("Finally, enter a value for 'C'(Ax^2+Bx+C):\n");        c = scan.nextDouble();        System.out.printf("Great! This is what your equation should look like:\n%.1fx^2 + %.1fx + %.1f = ",a,b,c);        double d = 2*a;        double e = ((a*a)-(4*a*c))/d;        double rt1 = (-b+(Math.sqrt(e)))/d;        double rt2 = (-b-(Math.sqrt(e)))/d;        System.out.printf("\nThe equation %.1fx^2 + %.1fx + %.1f has the following roots:\n", a, b, c);        System.out.printf("Root 1: %.1f\nRoot 2: %.1f", rt1, rt2);    }} 

 

Here's an online compiler link of it working. http://goo.gl/zQZqRl

Edited by Encoded

Share this post


Link to post
Share on other sites

You can change it back to the -b, I was thinking of something different. This appears to be a compiler problem.

 

Here's the output I got:

Hello, and welcome to Bailey quadratic root finder!Please enter a value for 'A'(Ax^2+Bx+C):1Now, please enter a value for 'B'(Ax^2+Bx+C):-2Finally, enter a value for 'C'(Ax^2+Bx+C):-2Great! This is what your equation should look like:1.0x^2 + -2.0x + -2.0 = The equation 1.0x^2 + -2.0x + -2.0 has the following roots:Root 1: 2.1Root 2: -0.1Process finished with exit code 0

Using this code:

 import java.util.Scanner;public class Test{    public static void main (String args[])    {        double a;        double b;        double c;        Scanner scan = new Scanner(System.in);        System.out.println("\nHello, and welcome to Bailey quadratic root finder!");        System.out.println("Please enter a value for 'A'(Ax^2+Bx+C):\n");        a = scan.nextDouble();        System.out.println("Now, please enter a value for 'B'(Ax^2+Bx+C):\n");        b = scan.nextDouble();        System.out.println("Finally, enter a value for 'C'(Ax^2+Bx+C):\n");        c = scan.nextDouble();        System.out.printf("Great! This is what your equation should look like:\n%.1fx^2 + %.1fx + %.1f = ",a,b,c);        double d = 2*a;        double e = ((a*a)-(4*a*c))/d;        double rt1 = (-b+(Math.sqrt(e)))/d;        double rt2 = (-b-(Math.sqrt(e)))/d;        System.out.printf("\nThe equation %.1fx^2 + %.1fx + %.1f has the following roots:\n", a, b, c);        System.out.printf("Root 1: %.1f\nRoot 2: %.1f", rt1, rt2);    }} 

 

Here's an online compiler link of it working. http://goo.gl/zQZqRl

Great....how should I go about trying to fix that...

Share this post


Link to post
Share on other sites

Great....how should I go about trying to fix that...

What IDE are you using?

Share this post


Link to post
Share on other sites

What IDE are you using?

Like what text editing software am I using? I'm not allowed to use Eclipse or anything like that because the class requires I use a straight text editing software. So I'm currently using Sublime Text.

Edited by baileyr12

Share this post


Link to post
Share on other sites

Try importing java.lang.Math.

 

I love you. I thought I tried this, but that fixed it! Thank you so much!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.