Parsing some complexNumber

• 07-09-2013, 12:06 AM
NoobieCode
Parsing some complexNumber
Hello. I need help with an algorithm on taking a String representation of a complexnumber in form a + bi or a - bi or -a + bi or -a-bi.

PseudoCode:

So my method passes a string str.

I first say str.replaceAll(" ", ""); //replaceing all spaces with no space.

int i = str.indexOf("+");
int endIndex = s.length() - 1;
String realString = s.substring(0, i); //everything from left of + sign is real number a.
String imagString = s.substring(i, endIndex); //everthing from right of + sign is imaginary number b.

return my number using Double.parseDouble

----------------

I was wondering how I would write it if there was a "-" sign before a AND/OR before b. Thank you for your time.
• 07-09-2013, 12:15 AM
jim829
Re: Parsing some complexNumber
Do it the same way you would do a + sign before b. And don't forget that it would be legal to have a + sign before a too. I would check out the regex API to help with this --> java.util.regex (Java Platform SE 7 )

Regards,
Jim
• 07-09-2013, 12:22 AM
NoobieCode
Re: Parsing some complexNumber
Sorry. I am new. What is this regex?
• 07-09-2013, 12:44 AM
jim829
Re: Parsing some complexNumber
Well, it may be slightly strange for some one new to it (and not just new to programming). But regular expressions are ways of matching patterns in strings using a specific syntax. These patterns are referred to as regular expressions. It might be easier at this junction to just use regular String parsing methods such as index, and substring to retrieve the components of your complex numbers.

But regular expressions are really worth knowing.

Regards,
Jim
• 07-09-2013, 12:56 AM
NoobieCode
Re: Parsing some complexNumber
Am I using this right?

str.matches ("+");
So a String str that has a "+" in it. And I need to do something condition checking using this right?
• 07-09-2013, 01:26 AM
jim829
Re: Parsing some complexNumber
To solve any problem in programming you need to use various tools of the language. My recommendation is to determine what you think you need to do the job. Then look for those methods (such as those in String) and write small pieces of code to see if you understand how they are working. Ubiquitous use of the System.out.println() method will certainly help. The + operator also has special meaning in certain contexts in regular expressions so it may need to be escaped. In this case the answer is no, it won't do what you want. First you need to prefix the + sign with two back slashes. The matches method will only return true if the pattern matches the entire string. So it won't find a + sign but simply tell you if the String matches one. If you want to find a specific character you can use String.indexOf(). It is important to read the API to know exactly what the method does.

Regards,
Jim
• 07-09-2013, 09:05 AM
JosAH
Re: Parsing some complexNumber
Maybe a bit simpler here:

Code:

```int i= string.lastIndexOf('+'); int j= string.lastIndexOf('-'); int p= Math.max(i, j);```
p is the index where the imaginary part of the number starts.

kind regards,

Jos (<--- hates regular expressions)
• 07-09-2013, 09:17 AM
gimbal2
Re: Parsing some complexNumber
Aw, what did regular expressions ever do to you?

I don't hate them, I only hate the fact that people want to build entire applications using them. Apparently where regex'es are concerned you are not supposed to reason if it is a good idea to use them or not, you just assume it is.
• 07-09-2013, 09:29 AM
JosAH
Re: Parsing some complexNumber
Quote:

Originally Posted by gimbal2
Aw, what did regular expressions ever do to you?

I don't hate them, I only hate the fact that people want to build entire applications using them. Apparently where regex'es are concerned you are not supposed to reason if it is a good idea to use them or not, you just assume it is.

That's exactly why I, should I say, dislike them. All they can do is recognize a regular language and sometimes they're even bad at that (backtracking); I use regular expressions in my vi, and sometimes in my code for relatively simple stuff, but that's it, I trust them as far as I can throw a rock at them ...

kind regards,

Jos
• 07-09-2013, 05:03 PM
jim829
Re: Parsing some complexNumber
Quote:

Originally Posted by JosAH
That's exactly why I, should I say, dislike them. All they can do is recognize a regular language and sometimes they're even bad at that (backtracking)

Well, I still like them (Perhaps it's because I used to do a lot of Perl programming before Java). And I do notice that the Java regex engine seems to handle backtracking better (or a least differently that Perl).

Regards,
Jim
• 07-09-2013, 05:52 PM
JosAH
Re: Parsing some complexNumber
Quote:

Originally Posted by jim829
Well, I still like them (Perhaps it's because I used to do a lot of Perl programming before Java). And I do notice that the Java regex engine seems to handle backtracking better (or a least differently that Perl).

The world is divided in two separate camps; one camp contains the people who love regular expressions, pocket protectors and broccoli and the other camp where the beautiful, intelligent people live and who hate regular expressions; they're surrounded by scantily clad women and chocolate sauce and booze and more of those women and and ...

kindest regards,

Jos ;-)