public class quadraticEquation{ public static String mainVar = ""; public static String var = ""; public static boolean foundVar = false; public static double[] factors = {0.0,0.0}; public static double a = 0.0; public static double b = 0.0; public static double c = 0.0; public static quadraticEquation e1 = null; public static quadraticEquation e2 = null; public static final String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; public quadraticEquation(String s){ if(s.contains("^2")){ String[] args = s.split("^2");//Error occurs here for(int i = 0; i < alphabet.length; i++){ if(args[0].contains(alphabet[i])){ var = alphabet[i]; foundVar = true; args[0] = args[0].replace(alphabet[i],""); try{ a = Double.parseDouble(args[0]); }catch(Exception e){ System.out.println(""+e); a = 0.0; } } } if(foundVar){ if(args[1].contains(var)){ args[1] = args[1].replace(var,"_"+var); String[] subArgs = args[1].split(var); if(subArgs.length == 2){ try{ if(subArgs[0].contains("_")){ b = Double.parseDouble(subArgs[0].replace("_","")); c = Double.parseDouble(subArgs[1]); } }catch(Exception e){ System.out.println(""+e); } }else if(subArgs.length == 1){ if(subArgs[0].contains("_")){ try{ b = Double.parseDouble(subArgs[0].replace("_","")); c = 0.0; }catch(Exception e){ System.out.println(""+e); } }else{ try{ b = 0.0; c = Double.parseDouble(subArgs[0]); }catch(Exception e){ System.out.println(""+e); } } } } } } } public static double absValue(double d){ if(d<0){ d = d * -1; } return d; } public static void solve(String s1, String s2){ e1 = new quadraticEquation(s1); e2 = new quadraticEquation(s2); if(e1.a > e2.a){ e1.a = e1.a - e2.a; e1.b = e1.b - e2.b; e1.c = e1.c - e2.c; e1.factor(); System.out.println("("+(-1*e1.factors[0])+","+(((e1.a*(-1*e1.factors[0]))+(e1.b*(-1*e1.factors[0])))+e1.c)+") ("+(-1*e1.factors[1])+","+(((e1.a*(-1*e1.factors[1]))+(e1.b*(-1*e1.factors[1])))+e1.c)+")"); }else{ e2.a = e2.a - e1.a; e2.b = e2.b - e1.b; e2.c = e2.c - e1.c; e2.factor(); System.out.println("("+(-1*e2.factors[0])+","+(((e2.a*(-1*e2.factors[0]))+(e2.b*(-1*e2.factors[0])))+e2.c)+") ("+(-1*e2.factors[1])+","+(((e2.a*(-1*e2.factors[1]))+(e2.b*(-1*e2.factors[1])))+e2.c)+")"); } } public static void main(String[] args){ solve("1x^2+0x-4","0x^2+5x-10"); } public static void factor(){ for(int i = 0; i < c; i++){ for(int j = 0; j < c; j++){ if(i*j == absValue(c)){ if(c>=0){ if(b>=0){ if(i+j == <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />{ factors[0] = i; factors[1] = j; } }else if((-1*i)+(-1*j) == <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />{ factors[0] = -1*i; factors[1] = -1*j; } }else if(c<0){ if((-1*i)+j == <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />{ factors[0] = -1*i; factors[1] = j; }else if(i+(-1*j) == <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />{ factors[0] = i; factors[1] = -1*j; } } } } } } }
Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

4 replies to this topic
#1
Posted 08 May 2012 - 09:03 PM
Okay, well, this code is something I'm working on to do my Systems of Quadratic Equations math homework. Every time I run it, though, it doesn't split the equation in half, at all, which causes parsing errors later on in the code. If you can tell me why in the heck it's not doing it, many kudos to you, because I can't figure it out for the life of me. Here is the code, and I added a problem from one of my sheets (2 formulas, glitches out before second one gets a chance to be worked). The error is a runtime error, so it wont show up in the compiler. Many thanks.
Speaks fluent Java
#2
Posted 08 May 2012 - 10:27 PM
Split function takes a regular expression as parameter.
In a regex '^' is a special letter meaning 'not' or 'everything but'.
You'll need to escape it with a backslash (2 in java)
In a regex '^' is a special letter meaning 'not' or 'everything but'.
You'll need to escape it with a backslash (2 in java)
#3
Posted 09 May 2012 - 03:50 AM
Ohhhhhh, okay, I get it now. Didn't know that, thanks

Speaks fluent Java
#4
Posted 09 May 2012 - 04:03 AM
You can make your life a bit easier with regexes by the way:
Run this:
This pattern requires exactly the form of ux^2+vx+w.
You can improve the regex with more brackets and question marks, making it more complex but giving your function more freedom.
For example to let you be able to leave parts of the function away:
If you want to be able to change the order, like vx+w+ux^2. Then in my opinion you're better of writing multiple patterns with different orders.
Or write a pattern per part, like to get 'a' out of the function:
Run this:
import java.util.regex.Matcher; import java.util.regex.Pattern; ... Pattern pattern = Pattern.compile("(-?\\d+)x\\^2\\+?(\\-?\\d+)x\\+?(\\-?\\d+)"); String input = "1x^2+0x-4"; Matcher matcher = pattern.matcher(input); if(matcher.matches()){ System.out.println("1x^2+0x-4\n---------"); System.out.println("a = " + matcher.group(1)); System.out.println("b = " + matcher.group(2)); System.out.println("c = " + matcher.group(3)); } String input2 = "0x^2-5x+-10"; Matcher matcher2 = pattern.matcher(input2); if(matcher2.matches()){ System.out.println("\n0x^2-5x+-10\n----------"); System.out.println("a = " + Integer.parseInt(matcher2.group(1))); System.out.println("b = " + Integer.parseInt(matcher2.group(2))); System.out.println("c = " + Integer.parseInt(matcher2.group(3))); }+ and - are also special regex characters, hence all the backslashes in the pattern.
This pattern requires exactly the form of ux^2+vx+w.
You can improve the regex with more brackets and question marks, making it more complex but giving your function more freedom.
For example to let you be able to leave parts of the function away:
public static void main(String[] args) { String input1 = "1x^2+0x-4"; String input2 = "0x^2-5x+-10"; String input3 = "-6x-5"; String input4 = "7x^2-8x"; String input5 = "-15x^2+4"; String input6 = "-54x^2+-4x+-40"; parseFunction(input1); parseFunction(input2); parseFunction(input3); parseFunction(input4); parseFunction(input5); parseFunction(input6); } private static void parseFunction(String function){ Pattern pattern = Pattern.compile("((-?\\d+)x\\^2\\+?)?((\\-?\\d+)x\\+?)?(\\-?\\d+)?"); Matcher matcher = pattern.matcher(function); if(matcher.matches()){ System.out.println(function + "\n-------------"); System.out.println("a = " + (matcher.group(2) == null ? "0" : matcher.group(2))); System.out.println("b = " + (matcher.group(4) == null ? "0" : matcher.group(4))); System.out.println("c = " + (matcher.group(5) == null ? "0" : matcher.group(5)) + "\n\n"); } }
If you want to be able to change the order, like vx+w+ux^2. Then in my opinion you're better of writing multiple patterns with different orders.
Or write a pattern per part, like to get 'a' out of the function:
Pattern.compile(".*(-?\\d+)x^2.*");And then group 1.
#5
Posted 09 May 2012 - 04:07 PM
Thanks, and that sounds cool. I'll look into it a bit more.

Speaks fluent Java
Also tagged with one or more of these keywords: form
General Forums →
General Programming →
Specifying PHP file path while creating an HTML formStarted by gvvishwanath, 30 Dec 2015 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Jquery Validation in Partial View Form Isn't TriggeringStarted by googaplex, 01 Jul 2015 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
how to deal with the forms indtified with idStarted by scseguanshui06, 20 Jul 2014 ![]() |
|
![]() |
||
Language Forums →
PHP →
what script do I run on a form?Started by kxx4, 12 May 2014 ![]() |
|
![]() |
||
Language Forums →
Visual Basic →
Send multipart form data. (Send strings and a file)Started by LegacyDash, 21 Feb 2014 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download