•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Help, I Don't Know Whats Wrong.

form

4 replies to this topic

### #1 Chall

Chall

• Senior Member
• 349 posts
• Location:Cedar Rapids, IA
• Programming Language:Java
• Learning:C, Java, C++, C#, Python, JavaScript, Assembly

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.

```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"};
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){

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;
}
}
}
}
}
}
}
```

• 1
Speaks fluent Java

### #2 wim DC

wim DC

Roar

• Expert Member
• 2681 posts
• Programming Language:Java, JavaScript, PL/SQL
• Learning:Python

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)
• 0

### #3 Chall

Chall

• Senior Member
• 349 posts
• Location:Cedar Rapids, IA
• Programming Language:Java
• Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 09 May 2012 - 03:50 AM

Ohhhhhh, okay, I get it now. Didn't know that, thanks
• 0
Speaks fluent Java

### #4 wim DC

wim DC

Roar

• Expert Member
• 2681 posts
• Programming Language:Java, JavaScript, PL/SQL
• Learning:Python

Posted 09 May 2012 - 04:03 AM

You can make your life a bit easier with regexes by the way:
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.
• 1

### #5 Chall

Chall

• Senior Member
• 349 posts
• Location:Cedar Rapids, IA
• Programming Language:Java
• Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 09 May 2012 - 04:07 PM

Thanks, and that sounds cool. I'll look into it a bit more.
• 0
Speaks fluent Java

### Also tagged with one or more of these keywords: form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download