Android : int Cannot resolve into a variable java

on Saturday, April 18, 2015


problem : when i'm trying to convert int into double it's showing an error that int cannot resolve into variable . This program will input quadratic equation as input and extracts the coefficient of aX2-bX-c=0 in this format and solve the quadratic equation. But it is some error in conversion from int to double.


Program :



public static String quad (final String equation)
{
final String regex = "([+-]?\\d+)X2([+-]\\d+)X([+-]\\d+)=0";
Pattern pattern = Pattern.compile(regex);


Matcher matcher = pattern.matcher(equation);

if (matcher.matches()) {
int a1 = Integer.parseInt(matcher.group(1));
int b1 = Integer.parseInt(matcher.group(2));
int c1 = Integer.parseInt(matcher.group(3));

// System.out.println("a=" + a + "; b=" + b + "; c=" + c);
}
double a = (double) a1; // error message a1 cannot resolve into variable
double b = (double) b1; // error message b1 cannot resolve into variable
double c = (double) c1; // error message c1 cannot resolve into variable


double r1 = 0;
double r2 = 0;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0){

// r = -b / 2 * a;

r1 = (-b + Math.sqrt(discriminant)) / (2 * a);
r2 = (-b - Math.sqrt(discriminant)) / (2 * a);

// System.out.println("Real roots " + r1 + " and " + r2);
}
if (discriminant == 0){
// System.out.println("One root " +r1);

r1 = -b / (2 * a);
r2 = -b / (2 * a);

}
if (discriminant < 0){
// System.out.println(" no real root");

}

String t1 = String.valueOf(r1);
String t2 = String.valueOf(r2);
String t3 ;
t3 = t1+" "+t2;
return t3;

}

0 comments:

Post a Comment