Java program to extract coefficients from a quadratic equation

Problem: The Java program splits the coefficients into a quadratic equation, for example if the input line

String str1; str1 = "4x2-4x-42=0" 

So I need to break down the coefficients from a given input string and get the output as

 a = 4 b = -4 c = -42 

I tried this:

 String equation = "ax2+bx-c=0"; String[] parts = equation.split("\\+|-|="); for (int i = 0; i < parts.length - 2; i++) { String part = parts[i].toLowerCase(); System.out.println(part.substring(0, part.indexOf("x"))); } System.out.println(parts[2]); 

But I got the result as 23x2 and 4x and 4. The required actual result is 23 ,- 4 , 4 .

+7
java string math regex
source share
5 answers

Use Regex, the following template will work:

 ([+-]?\d+)[Xx]2\s*([+-]?\d+)[Xx]\s*([+-]?\d+)\s*=\s*0 

This will match the quadratic and retrieve the parameters, allows you to decide how it works:

  • (...) this is a capture group
  • [+-]?\d+ this corresponds to several digits preceded by the + or - option
  • [Xx] this matches "x" or "x"
  • \s* this matches zero or more spaces

So,

  • ([+-]?\d+) matches the argument "a"
  • [Xx]2 matches "X2" or "x2"
  • \s* matches an optional space
  • ([+-]?\d+) matches the argument "b"
  • [Xx] matches "X" or "x"
  • \s* matches an optional space
  • ([+-]?\d+) matches the argument "c"
  • \s*=\s*0 matches "= 0" with some optional spaces

Lets wrap this in a class :

 private static final class QuadraticEq { private static final Pattern EQN = Pattern.compile("([+-]?\\d+)[Xx]2\\s*([+-]?\\d+)[Xx]\\s*([+-]?\\d+)\\s*=\\s*0"); private final int a; private final int b; private final int c; private QuadraticEq(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public static QuadraticEq parseString(final String eq) { final Matcher matcher = EQN.matcher(eq); if (!matcher.matches()) { throw new IllegalArgumentException("Not a valid pattern " + eq); } final int a = Integer.parseInt(matcher.group(1)); final int b = Integer.parseInt(matcher.group(2)); final int c = Integer.parseInt(matcher.group(3)); return new QuadraticEq(a, b, c); } @Override public String toString() { final StringBuilder sb = new StringBuilder("QuadraticEq{"); sb.append("a=").append(a); sb.append(", b=").append(b); sb.append(", c=").append(c); sb.append('}'); return sb.toString(); } } 

Pay attention to \\ , this is required by Java.

Quick test:

 System.out.println(QuadraticEq.parseString("4x2-4x-42=0")); 

Output:

 QuadraticEq{a=4, b=-4, c=-42} 
+3
source share

You can use regex as follows:

 final String regex = "([+-]?\\d+)x2([+-]\\d+)x([+-]\\d+)=0"; Pattern pattern = Pattern.compile(regex); final String equation = "4x2-4x-42=0"; Matcher matcher = pattern.matcher(equation); if (matcher.matches()) { int a = Integer.parseInt(matcher.group(1)); int b = Integer.parseInt(matcher.group(2)); int c = Integer.parseInt(matcher.group(3)); System.out.println("a=" + a + " b=" + b + " c=" + c); } 

Output:

 a=4 b=-4 c=-42 
+1
source share

If you use only the box:

 int xsqrd = equation.indexOf("x2"); int x = equation.indexOf("x", xsqrd); int equ = equation.indexOf("="); String a = equation.subString(0,xsqrd); String b = equation.subString(xsqrd+1,x); String c = equation.subString(x,equ); 

I may have messed up the substrings, but you got the general idea.

0
source share

First note: if you use a character as a delimiter in regexp, you will lose it in slpitted elements. I suggest you use the following regexp:

 "x2|x|=" 

Then you can get only numbers. Full code snippet:

 public class Main { private static final char[] varNames = {'a', 'b', 'c'}; public static void main(String[] args) { String equation = "4x2-4x-42=0"; String[] parts = equation.split("x2|x|="); // you will get 4 elements, but the last element is always 0 for(int i=0; i<parts.length - 1; i++){ System.out.println(varNames[i] + " = " + Integer.parseInt(parts[i])); } } } 

But in this case you will have "+" characters on the output. To avoid this, you can use Integer.parseInt(parts[i]) instead of parts[i] .

0
source share
 for ( int i = 0 ; i < str.length ; ++i ){ if(asciiOf(str.charAt(i)) == asciiOfIntegerValue ){ addCharInArrayList(str.charAt(i)); }else{ addInFuncList(); addCharInArrayList("_"); } // join numbers which are not separated by _ and apply BODMAS rule and solve it // fyi : apologies - very inefficient pseudocode, wrote in a haste 
0
source share

All Articles