Results 1 to 20 of 22
- 02-22-2011, 03:49 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Java method that check brackets in String are matched?
How to write balancedBracketsByCounting(String s), as follows, that takes a string as an input and checks whether the brackets "[", "]", "(", and ")" in the string are matched correctly?
balancedBracketsByCounting("[x][[xy]]") would return True.
balancedBracketsByCounting("[[x[y]]") would return False.
Do we need to use Stack and Queue in our program?
Please assist, thanks.
- 02-22-2011, 04:29 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Count them?
- 02-22-2011, 05:08 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-22-2011, 05:13 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Oh.
Different ones in the same string.
I was going by the OPs example...counting would work well enough there.
:)
- 02-22-2011, 05:18 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 02-22-2011, 05:28 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
and for usageboolean brackets(String string, char start, char end){
boolean check = false;
boolean isBalance = true;
for(char x: string.toCharArray()){
if(x == start && !check){
check = true;
}else if(x == start && check){
isBalance = false;
}else if(x == end && !check){
isBalance = false;
}else if(x == end && check){
check = false;
}
}
return isBalance;
}brackets("(thisisTest)", '(', ')');
- 02-22-2011, 06:37 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 02-23-2011, 08:15 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
A spoonfeed that doesn't work?
Who'd have thought it, eh?
:)
- 02-23-2011, 08:17 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 02-23-2011, 09:06 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
- 02-23-2011, 09:08 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Thanks Josah for enlightenment ...
Does this work?
=========================================
import java.util.*;
public class balancedBraces
{
public boolean balancedBracesMethod (String s)
{
int myLength = s.length();
Stack st = new Stack();
char d;
for (int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if (c == '(')
{
st.push(new Character(c));
}
else if (c == '[')
{
st.push(new Character(c));
}
else if (c == ')')
{
if (st.empty() == true)
{
return false;
}
else if (st.empty() == false)
{
d = (Character) st.pop();
if (d != '(')
{
return false;
}
}
}
else if (c == ']')
{
if (st.empty() == true)
{
return false;
}
else if (st.empty() == false)
{
d = (Character) st.pop();
if (d != '[')
{
return false;
}
}
}
}
return true;
}
}Last edited by neonz; 02-23-2011 at 09:14 AM.
- 02-23-2011, 09:20 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Use code tags for starters.
Also wouldn't it be easier for you to actually run this with some test code than ask us to read through it and try and spot mistakes?
I will say that:
should be:Java Code:if (st.empty() == true) { ... } else if (st.empty() == false) { ... }
Comparing a boolean to true or false is is unecessary,Java Code:if (st.empty()) { ... } else { ... }
- 02-23-2011, 12:48 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
- 02-23-2011, 01:00 PM #14
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
public static void main (String args [])
{
String s = "( [ ] )";
boolean result = balancedBracesMethod (s);
System.out.print(result);
}
Have done this test with String s used.
Program seems to be working fine.
)([] return false
[()()] return true
[)()(] return false
()][ return false
Result return are expected.
- 02-23-2011, 01:21 PM #15
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-23-2011, 01:39 PM #16
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Sorry, was trying to get fast response and resolve the problem.
Another testing done. This time round is smooth testing. All the way test by entering new figure instead of hard-coding ...
public static void main (String args [])
{
while(true)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter test data: ");
try
{
String input = reader.readLine();
boolean result = balancedBracesMethod (input);
System.out.println(result);
}
catch (IOException e)
{
System.out.println("An unexpected error occured.");
}
}
}
Hope I can get feed back on improving the program code, and make improvement.
- 02-23-2011, 03:07 PM #17
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Latest program:
[highlight=Java]
import java.util.*;
import java.io.*;
public class balancedBraces
{
public static boolean balancedBracesMethod (String s)
{
int myLength = s.length();
Stack st = new Stack();
char d;
for (int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if (c == '(')
{
st.push(new Character(c));
}
else if (c == '[')
{
st.push(new Character(c));
}
else if (c == '{')
{
st.push(new Character(c));
}
else if (c == ')')
{
if (st.empty())
{
return false;
}
else
{
d = (Character) st.pop();
if (d != '(')
{
return false;
}
}
}
else if (c == ']')
{
if (st.empty())
{
return false;
}
else
{
d = (Character) st.pop();
if (d != '[')
{
return false;
}
}
}
else if (c == '}')
{
if (st.empty())
{
return false;
}
else
{
d = (Character) st.pop();
if (d != '{')
{
return false;
}
}
}
}
if (st.empty()) return true;
else return false;
}
public static void main (String args [])
{
boolean test = true;
while(test)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter test data: ");
try
{
String input = reader.readLine();
boolean result = balancedBracesMethod (input);
System.out.println(result);
}
catch (IOException e)
{
System.out.println("An unexpected error occured.");
}
}
}
}
[/highlight]
Please help to comment and check if there any problem.
Cross-posted: balancedBracketsByCounting (String s) that checks brackets are matched in the String.
- 02-23-2011, 03:11 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 02-24-2011, 06:09 AM #19
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Hi JosAH and KevinWorkman,
Any problem with my above mini-java program?
I have been fine-tuning it, to the last version alry.
Any other way to code the java program such that it is shorter or more efficient?
Please assist, thanks.
So far no one really got back to me on the java program, if there is any error any where, please do tell me.
Thank.
- 02-28-2011, 05:53 PM #20
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
check string inside string
By alacn in forum New To JavaReplies: 8Last Post: 07-07-2010, 12:52 PM -
How to check whether the string contains only the specified characters ????
By j_kathiresan in forum New To JavaReplies: 1Last Post: 04-30-2010, 03:21 PM -
Check method
By XMarkoX in forum New To JavaReplies: 5Last Post: 11-24-2009, 12:54 PM -
flipping memory cards if they are not matched...
By yanipao in forum New To JavaReplies: 8Last Post: 10-18-2009, 02:25 AM -
check command method please
By dirtycash in forum New To JavaReplies: 1Last Post: 12-06-2007, 09:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks