package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
double a, b, c;
double x1, x2;
System.out.println("Ingresa el valor de a: ");
a = sc.nextDouble();
System.out.println("Ingresa el valor de b: ");
b = sc.nextDouble();
System.out.println("Ingresa el valor de c: ");
c = sc.nextDouble();
try {
x1 = ((-b) - (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / 2 * a;
x2 = ((-b) + (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / 2 * a;
System.out.println("x1: " + x1 + ", x2: " + x2);
} catch (Exception e) {
System.out.println("Hay un problema");
}
}
}

- 100,966
- 191
- 140
- 197
-
I didn't got the question. Did you mean in which case you will get an exception ? – Deepak Patankar May 28 '22 at 04:42
-
give an example of what is the input you are getting exception of. – Sayan Bhattacharya May 28 '22 at 04:43
-
I have to obtain a exception with the Quadratic Formula setting the a with 0 value. That makes the square root didn't work because the division can't work with a 0. – Gerardo David Gonzalez Nuñez May 28 '22 at 04:53
-
Sorry if my English is not good. Im starting learn. – Gerardo David Gonzalez Nuñez May 28 '22 at 04:54
-
See this answer: [Java division by zero doesnt throw an ArithmeticException - why?](https://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why#:~:text=Java%20will%20not%20throw%20an,the%20result%20will%20be%20INFINITY.) – hokwanhung May 28 '22 at 05:16
-
1Please explain the problem and what kind of solution you're looking for in the body of your question. – Mark Rotteveel May 28 '22 at 06:42
3 Answers
B is value to get square root from answer , then you want to throw exception.
Example: √0^2 = 0, then yeah not divisible.
Solution:
use If-else statement and throw exceptions(custom or default).
//If square root B is 0, then Throw error
try {
if(b == 0){
throw new Exception();
}
else {
x1 = ((-b) - (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / 2 * a;
x2 = ((-b) + (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / 2 * a;
System.out.println("x1: " + x1 + ", x2: " + x2);
}
} catch (Exception e) {
System.out.println("There is A Problem");
}
I replicated It, and this is bonus for you: https://onlinegdb.com/9oqNMLySU
Give a thumbs up if it helps. Gladge

- 16
- 2
Arithmetic with double
values does not throw exceptions. Nor does taking a square root of zero or a negative number. So your code going to have to test for specific inputs (or results) and throw an exception explicitly.
Hints:
read the javadocs for
Math.sqrt(double)
andDouble.isNaN(double)
read about what
NaN
means in floating point arithmetic,read about √-1 and complex numbers (if you have forgotten your high-school math classes),
do some algebra to figure out what values cause the quadratic formula to produce complex numbers as the "roots".

- 698,415
- 94
- 811
- 1,216
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int a, b, c;
int x1, x2, divisor, dividendo1, dividendo2, raiz, resta, cuadrado;
System.out.println("Ingresa el valor de a: ");
a = sc.nextInt();
System.out.println("Ingresa el valor de b: ");
b = sc.nextInt();
System.out.println("Ingresa el valor de c: ");
c = sc.nextInt();
cuadrado = b * b;
resta = cuadrado - 4 * a * c;
raiz = (int)Math.sqrt(resta);
if (raiz==0) {
throw new ArithmeticException();
}
dividendo1 = -b - raiz;
dividendo2 = -b + raiz;
divisor = 2 * a;
x1 = dividendo1 / divisor;
x2 = dividendo2 / divisor;
System.out.println("x1: " + x1 + ", x2: " + x2);
}
}
I got it!
-
Actually, this solution is incorrect for two reasons. 1) It produces inaccurate results if `sqrt(b^2 -4ac)` is non-integer. 2) The case where `sqrt(b^2 -4ac)` is exactly zero should not throw an exception. – Stephen C May 28 '22 at 07:57
-
(Do you understand why you need to cast the `double` to an `int`? What it is actually doing ... in the NaN case?) – Stephen C May 28 '22 at 08:03
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '22 at 13:51