Results 1 to 11 of 11
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Parsing some complexNumber

    Sorry. I am new. What is this regex?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default Re: Parsing some complexNumber

    Maybe a bit simpler here:

    Java 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)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,371
    Rep Power
    6

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default Re: Parsing some complexNumber

    Quote Originally Posted by gimbal2 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default Re: Parsing some complexNumber

    Quote Originally Posted by JosAH View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default Re: Parsing some complexNumber

    Quote Originally Posted by jim829 View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Parsing XML
    By techie in forum XML
    Replies: 2
    Last Post: 02-07-2011, 09:57 AM
  2. ComplexNumber.java I NEED HELPPP
    By db7_turbo in forum Advanced Java
    Replies: 4
    Last Post: 04-14-2010, 04:16 PM
  3. XML parsing using DOM
    By alley in forum XML
    Replies: 4
    Last Post: 10-14-2009, 07:17 PM
  4. Xml Parsing
    By Nomad in forum XML
    Replies: 12
    Last Post: 02-22-2009, 12:19 PM
  5. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 12:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •