Results 1 to 12 of 12
  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Where's the logic?

    Where is the logic in:
    Java Code:
    split("a?b?gf#e", "[?#]") returns a, b, ?, b, gf, #, e
    Need to implement my own split method, and above is example what it should return, but can not see any logic.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Where's the logic?

    Since split is based upon a regular expression, are you referring to the logic of Regular Expressions?
    See Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    But why reinvent the wheel?

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Where's the logic?

    Is is just exercise, and can't see any logic in result.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Where's the logic?

    Read the link - the logic is in the regular expression.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Where's the logic?

    Quote Originally Posted by diamonddragon View Post
    Is is just exercise, and can't see any logic in result.
    On what are you calling the split( ... ) method? If I do this:

    Java Code:
    import java.util.Arrays;
    
    
    public class T {
    	public static void main(String[] args) {
    		String[] r= "a?b?gf#e".split("[?#]");
    		
    		System.out.println(Arrays.toString(r));
    	}
    }
    ... it doesn't give me any surprise.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Where's the logic?

    Quote Originally Posted by JosAH View Post
    On what are you calling the split( ... ) method? If I do this:

    Java Code:
    import java.util.Arrays;
    
    public class T {
    	public static void main(String[] args) {
    		String[] r= "a?b?gf#e".split("[?#]");
    		
    		System.out.println(Arrays.toString(r));
    	}
    }
    ... it doesn't give me any surprise.

    kind regards,

    Jos
    Return should consist of regex, so, return should be exactly: a, b, ?, b, gf, #, e
    Not a, b, gf, e.

    Requested array consist of b,b and regular output gives only one b.
    Last edited by diamonddragon; 01-27-2012 at 09:54 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Where's the logic?

    Quote Originally Posted by diamonddragon View Post
    Return should consist of regex, so, return should be exactly: a, b, ?, b, gf, #, e
    Not a, b, gf, e.

    Requested array consist of b,b and regular output gives only one b.
    Can you explain what you're saying here, because you've lost me completely. From what Jos's code looks like, the output should be

    Java Code:
    [a, b, gf, e]
    Perhaps you need to spell out more clearly just what you're trying to do including what part of your bit above is supposed to change -- your result or your code. If your code, I don't see how you can get the output you expect via regex (or any means as I don't see how the output is tied logically to the String entered).
    Last edited by Fubarable; 01-27-2012 at 11:22 PM.

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Where's the logic?

    Need to implement own split method, such that
    Java Code:
    split("a?b?gf#e", "[?#]")
    will return exactly:
    Java Code:
    [a, b, ?, b, gf, #, e]
    That is requested, and my question is where is the logic in that output?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Where's the logic?

    But again, what are the constants and what are the variables?

    For example, are the constants the input String "a?b?gf#e" and the output [a, b, ?, b, gf, #, e]? Is this unchangeable? And is the thing you must change the regex String used in the Split? The "[?#]" part?

    And what do you mean "implement own split method"? Do you mean you need to write a method that replaces String's split method or that you must use String's split method to achieve your result?

    Again, you're still leaving a lot unsaid. Please assume that we can't read your mind.

  10. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Where's the logic?

    Say:
    Java Code:
    public class StringSplitMethod {
    
        public static void main(String[] args) {        
            String[] r;
            r = split("a?a#b?gf#e", "?");         
            System.out.println(Arrays.toString(r));
            r = split("a?a#b?gf#e", "[?#]");         
            System.out.println(Arrays.toString(r));
        }
                
        public static String[] split(String s, String regex) {
            
        }
    
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Where's the logic?

    OK, well good luck with that.

  12. #12
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

Similar Threads

  1. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-22-2012, 11:24 PM
  2. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 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
  •