| | |
tuff question in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
I need to create a static boolean method, boolean isValid(String str) which gets a string and check the brackets in it...
a correct expression is an expression as one of the following:
1) an empty string or a string that includes space only in it.
2) st1 +"" +st2, which st1 and st2 are correct bracket expressions.
3) ("+st+") or ["+st+"] or {"+st+"} which st is a correct bracket expression.
examples:
isValid(" ") == true
isValid("() [] ") == true
isValid("{] [}") == false
Another condition is that the complexity must be linear which says O(0).
Is anyone please can write me a try of that method ?
Thanks !
Adam.
a correct expression is an expression as one of the following:
1) an empty string or a string that includes space only in it.
2) st1 +"" +st2, which st1 and st2 are correct bracket expressions.
3) ("+st+") or ["+st+"] or {"+st+"} which st is a correct bracket expression.
examples:
isValid(" ") == true
isValid("() [] ") == true
isValid("{] [}") == false
Another condition is that the complexity must be linear which says O(0).
Is anyone please can write me a try of that method ?
Thanks !
Adam.
Last edited by Adami; Jan 16th, 2008 at 10:46 am.
Try using str.charAt(i) in a for-loop. This method returns one by one each char of the String. Then you can count the different brackets that open and close and make sure that each time the bracket count must be positive or zero. At the end of the loop all bracket counts must be zero. You must also check that you don't have a case such as this which is wrong :
{ [ } ]
{ [ } ]
Java Syntax (Toggle Plain Text)
public static boolean isValid(String str) { //code here }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Try using str.charAt(i) in a for-loop. This method returns one by one each char of the String. Then you can count the different brackets that open and close and make sure that each time the bracket count must be positive or zero. At the end of the loop all bracket counts must be zero. You must also check that you don't have a case such as this which is wrong :
{ [ } ]
Java Syntax (Toggle Plain Text)
public static boolean isValid(String str) { //code here }
thanks!
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
Let's see what you've tried. Easiest method would be to just use regular expressions.
The method javaAddict describes is very simple. When you encounter an opening bracket, increment your bracket counter. When you encounter a closing bracket, decrement the counter. If the counter is 0 at the end of the FOR loop, then the equation should be valid.
The method javaAddict describes is very simple. When you encounter an opening bracket, increment your bracket counter. When you encounter a closing bracket, decrement the counter. If the counter is 0 at the end of the FOR loop, then the equation should be valid.
In case you have something like this: { [ } ], perhaps you should have some boolean variables for the different brackets(isCloded, isOpened) so you can check that you cannot close something if you are inside a different unclosed bracket. Try to remember the most recent open bracket
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
In case you have something like this: { [ } ], perhaps you should have some boolean variables for the different brackets(isCloded, isOpened) so you can check that you cannot close something if you are inside a different unclosed bracket. Try to remember the most recent open bracket
![]() |
Other Threads in the Java Forum
- Previous Thread: What the following method does ?
- Next Thread: For loop problem
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working






