Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Junky

Thread: How to make my program NOT dependent on spaces between characters?

  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default How to make my program NOT dependent on spaces between characters?

    I wrote a program that evaluates expressions with complex numbers in postfix format. It works perfectly but it is dependent on the expression having spaces between characters such as:
    ( 2 + 3 i ) ( 1 + 10 i ) +

    but I would like to make it also work if (2+3i)(1+10i)+ is entered and I have not been able to figure it out.
    Last edited by dragstang86; 06-30-2011 at 03:42 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,890
    Rep Power
    25

    Default

    You need to beef up your getNextToken logic not to require spaces as delimiters.

    Where is your getNextToken code? Looks like you are using StringTokenizer.
    Make it a class/method that works like an Enumeration: pass it a String and it parses out the next token and returns it.

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default

    I am currently using StringTokenizer's nextToken() method.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    As I said in your other thread you can read each char one at a time and work out yourself when you have reached the end of a token. Otherwise this might help:
    Java Code:
    import java.util.StringTokenizer;
    
    class Test {
        public static void main(String[] args) {
            String data = "(2+3i)(1+10i)";
            StringTokenizer st = new StringTokenizer(data, "()+", true);
            while(st.hasMoreTokens()) {
                System.out.println(st.nextToken());
            }
        }
    }
    You would have to add other operators to the delimiter list as needed. Also it will find 3i and 10i as a single token. If this is not what you want then it would be a simple matter of doing further processing in those cases.
    dragstang86 likes this.

  5. #5
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default

    Beautiful, that's exactly the way I was trying to figure out. Thanks a ton!

Similar Threads

  1. Replies: 0
    Last Post: 03-03-2011, 03:20 PM
  2. Replies: 0
    Last Post: 08-07-2009, 12:00 PM
  3. Platform dependent File Path....
    By sanjayksaini in forum Advanced Java
    Replies: 7
    Last Post: 11-30-2008, 04:12 AM
  4. Trying to make a GUI program
    By GettinGwap in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 12:37 AM
  5. ERROR: Can't find dependent libraries
    By baltimore in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:37 AM

Posting Permissions

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