Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,159 PointsRegex for complex numbers (Java)
I have a regex that recognizes most complex number cases but makes a couple of serious mistakes
1) it identifies "i" and "-i" as having 0.0 coefficient
2) it calls "not a match" for numbers of this format
real number + i real number - i
here is my regex (below)
do you know how I can tweak is so it will handle all complex and imaginary numbers including those that involve an i that has a 1 or - 1 coefficient (therefore, -i or i)
hope I made that clear....
Nancy M.
Pattern p = Pattern.compile("(?<real>-?\\d*[.]?\\d+)??((?<imag>[+-]?\\d*[.]?\\d+)i|i|-i)?$");
3 Answers
Craig Dennis
Treehouse TeacherHi Nancy!
Fun problem! What about using a library like Apache Commons Math? Would this parse method do the trick?
Can you use an external lib?
Nancy Melucci
Courses Plus Student 36,159 PointsI will check out the library/method. I am very very very slow at this. So, I appreciate your patience and help. It is fun. Tough fun. I'll keep you posted.
Nancy M.
Nancy Melucci
Courses Plus Student 36,159 PointsUpdate: Per Regexr (http://regexr.com/) I have the correct regex for the cases such as
2+i
-66+i
7-i
It's /[-1-9][0-9]+[+-]i/
It isn't working in my switch statement though... I'll keep working on it.
Nancy
Nancy Melucci
Courses Plus Student 36,159 PointsUpdate:
I got some help from the professor last week. I am sure he was trying to help me when he rearranged my code and encouraged me to try again but now it works worse and I am even more lost trying to get it to run all the complex and imaginary cases correctly. I can't even get it back to where I had it last week.
This is the array of data I am running:
String[] data = {"i", "-i", "2+i", "-2+i", "2-i", "-2+i", "2+2i", "2-2i", "2i", "-2i", "splunge"};
Here is what it gives me back in the output:
run: Real: 0.0, imag: 1.0
Real: 0.0, imag: 1.0
Real: 0.0, imag: -1.0
Real: 2.0, imag: -1.0
Real: -2.0, imag: -1.0
Real: 2.0, imag: -1.0
Real: -2.0, imag: -1.0
Real: 2.0, imag: -1.0
Real: 2.0, imag: -1.0
Real: 0.0, imag: -1.0
Real: 0.0, imag: -1.0
Not a match
BUILD SUCCESSFUL (total time: 25 seconds)
The code follows. I don't know how else to learn exactly what the regex means without getting someone to tell me why all my i's are being identified as having coefficients of negative 1 and why this runs worse than what I had. I should have saved my version before letting him rearrange it. >< The rest of the class is going to be 5 program ahead of me soon. Despair.
public class ComplexTrialRun {
private double real;
private double imag;
public static void main(String [] args){
String[] data = {"i", "-i", "2+i", "-2+i", "2-i", "-2+i", "2+2i", "2-2i", "2i", "-2i", "splunge"};
int i = 0;
ComplexTrialRun c = new ComplexTrialRun();
ComplexTrialRun result = c.setComplex(data[i]);
for(i = 0; i<data.length; i++){
c.setComplex(data[i]);
}
}
public ComplexTrialRun setComplex( String word)
{
Pattern p = Pattern.compile("(?<real>-?\\d*[.]?\\d+)??(?<imag>[+-]?\\d*[.]?\\d*i)|"
+"(?<imagOnly>^[-+]i|-i\\d*\\?\\d*i$)[-]i|i$(?<realimag>^\\d+[-]i$)");
Matcher m = p.matcher(word);
if(m.find() ) {
//try{
String r = m.group("real");
String i = m.group("imag");
//"i", "-i", "2+i", "-2+i", "2-i", "-2+i", "2+2i", "2-2i", "2i", "-2i", "splunge"
real = (r != (String)null? Double.parseDouble(r): 0);
try{
if (i==null){
real = (r != (String)null? Double.parseDouble(r): 0);
}else {
switch (word) {
case "i":
imag = 1.0;
break;
case "-i":
imag = -1.0;
break;
case "[-1-9][1-9]+[-]i$":
real = (r != (String)null? Double.parseDouble(r): 0);
imag = 1.0;
break;
case "[-1-9][1-9]+[+]i$":
real = (r != (String)null? Double.parseDouble(r): 0);
imag = -1.0;
break;
case "[-1-9][1-9]i+[+-]i$":
real = (r != (String)null? Double.parseDouble(r): 0);
imag = 1.0;
System.out.println("This also sucks!");
break;
}
}
}catch(InputMismatchException ime){
System.out.println("Wrong format");
// i = m.group("imagOnly");
imag = (i != (String)null? Double.parseDouble(i): 0); //try and catch i
}
System.out.println("Real: " + real + "," + "\timag: " + imag);
}else{ System.out.println("Not a match");
}
return this;
}
}
Nancy Melucci
Courses Plus Student 36,159 PointsNancy Melucci
Courses Plus Student 36,159 PointsUpdate:
I figured out how to get it to recognize i and -i by using a switch case. I still cannot figure out how to make regex work for expressions such as "2+i" and "2-i"....here is what I have which works for everything except a real numbers combined with i and - i.
Just so you know I really am struggling with this and not just trying to get free help...
thanks everyone