# Where's the logic?

• 01-27-2012, 09:54 PM
diamonddragon
Where's the logic?
Where is the logic in:
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.
• 01-27-2012, 10:10 PM
doWhile
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?
• 01-27-2012, 10:25 PM
diamonddragon
Re: Where's the logic?
Is is just exercise, and can't see any logic in result.
• 01-27-2012, 10:28 PM
doWhile
Re: Where's the logic?
Read the link - the logic is in the regular expression.
• 01-27-2012, 10:32 PM
JosAH
Re: Where's the logic?
Quote:

Originally Posted by diamonddragon
Is is just exercise, and can't see any logic in result.

On what are you calling the split( ... ) method? If I do this:

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
• 01-27-2012, 10:50 PM
diamonddragon
Re: Where's the logic?
Quote:

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

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.
• 01-28-2012, 12:15 AM
Fubarable
Re: Where's the logic?
Quote:

Originally Posted by diamonddragon
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

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).
• 01-28-2012, 12:58 AM
diamonddragon
Re: Where's the logic?
Need to implement own split method, such that
Code:

`split("a?b?gf#e", "[?#]")`
will return exactly:
Code:

`[a, b, ?, b, gf, #, e]`
That is requested, and my question is where is the logic in that output?
• 01-28-2012, 01:08 AM
Fubarable
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.
• 01-28-2012, 01:36 AM
diamonddragon
Re: Where's the logic?
Say:
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) {             } }```
• 01-28-2012, 03:35 AM
Fubarable
Re: Where's the logic?
OK, well good luck with that.
• 01-28-2012, 05:16 AM
diamonddragon
Re: Where's the logic?
Does it mean there's no logic?