Results 1 to 10 of 10
Thread: matching parentheses stack
- 10-04-2010, 06:12 AM #1
Member
- Join Date
- Oct 2010
- Location
- Baltimore, Maryland
- Posts
- 5
- Rep Power
- 0
matching parentheses stack
Hello,
I'm new to Java and I have to write a program that reads in a string and matches the grouping symbols. If they don't match it has to return false and the position where the mismatch occurred. I got everything except the positioning part. That's where I'm stuck.
Any help would be appreciated.
here is my code:
import java.util.*;
import java.util.Scanner;
import java.util.Stack;
public class Assignment3a
{
private static final char LEFTPARENT = '(';
private static final char RIGHTPARENT = ')';
private static final char LEFTBRACE = '{';
private static final char RIGHTBRACE = '}';
private static final char LEFTBRACKET = '[';
private static final char RIGHTBRACKET = ']';
//declaring Parenthesis, Curly Braces and Brackets (left and right)
public static boolean isBalanced(String s) //if the input string is balanced (true/false)
{
Stack<Character> stack = new Stack<Character>();//
int c=0;
for (int i = 0; i < s.length(); i++) //reading the string into the loop
{
if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT); //push the first left parenthesis onto the stack
else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE); //push the first left Curly Brace onto the stack
else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET); //push the first left Bracket onto the stack
else if (s.charAt(i) == RIGHTPARENT)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTPARENT)
return false;
}
else if (s.charAt(i) == RIGHTBRACE)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACE)
return false;
}
else if (s.charAt(i) == RIGHTBRACKET)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACKET)
return false;
}
// all other characters will be ignored
}
return stack.isEmpty();
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a string with Parentheses, Brackets and curly braces: ");
String s = input.next();
{
if (isBalanced(s)== true)
System.out.println("The Parenthesis in"+" \""+ s +"\" "+"match");
if (isBalanced(s)== false)
System.out.println("The Parenthesis in"+" \""+ s +"\" "+ "do not match");
System.out.println();
}
}
}
- 10-04-2010, 07:21 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
First tip would be to post your code in code tags, it makes it more readable. Next, to report the location where the input is erroneous, I'd change your method signature to int instead of boolean, and instead of returning false, you return the index, if the input is valid, return -1.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-04-2010, 07:48 AM #3
Member
- Join Date
- Oct 2010
- Location
- Baltimore, Maryland
- Posts
- 5
- Rep Power
- 0
Thanks for the quick reply m00nchile. Sorry, that was my first post. My next one I will post in code tags. I forgot to mention that I have to use boolean. That's what makes it so confusing for me.
- 10-04-2010, 08:35 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 10-04-2010, 09:48 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Another way to circumvent the boolean requirement and a member variable would be to just print out the index where parsing wasn't succesfull:
Of course, this is assuming you don't need the parse error location anywhere else. And I agree with JosAH, programming classes can have very silly requirements for their assignments. And also, JosAHs observation on double parsing:Java Code:if(conditionNotMet) { System.out.println("Parse error at "+i); return false; }
Java Code:if (isBalanced(s)) //already a boolean, don't need the == true System.out.println("The Parenthesis in"+" \""+ s +"\" "+"match"); else //don't need to check again System.out.println("The Parenthesis in"+" \""+ s +"\" "+ "do not match"); System.out.println();Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-04-2010, 06:05 PM #6
Member
- Join Date
- Oct 2010
- Location
- Baltimore, Maryland
- Posts
- 5
- Rep Power
- 0
Thanks JosAH and m00nchile. I know this sounds a bit stupid, but I'm really new to Java. How do I make "i" a member and where do I place that if-statement then? I fixed the double parsing and learned something again.
- 10-04-2010, 06:10 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 10-05-2010, 02:47 AM #8
Member
- Join Date
- Oct 2010
- Location
- Baltimore, Maryland
- Posts
- 5
- Rep Power
- 0
That was a great hint from you guys. Thank You m00nchile and Dank U wel JosAH. I made "i" private, but how do I retrieve the position in main?
- 10-05-2010, 06:32 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Make a getter method to fetch the i variable:
I usually don't post solutions, but you obviously know a bit of programming, and things like these are just semantic issues.Java Code:public class Example { private int i; //constructors... methods... public int getI() { return i; } } public class ExampleTest { public static void main(String[] args) { Example e = new Example(); //do the parsing //if input is erroneous System.out.println("Mistake at " + e.getI()); } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-05-2010, 10:37 PM #10
Member
- Join Date
- Oct 2010
- Location
- Baltimore, Maryland
- Posts
- 5
- Rep Power
- 0
Thanks for your help m00nchile. I'm coming from C. I didn't know that Java is so much different and more advanced than C. However, I appreciate your efford to help me out on this one.It seems everything I try, I get a compiler error. I just turn it in the way it is. At least the boolean is working.
Thank you for your time and patience.
Similar Threads
-
Using regex to retrieve all text inside parentheses
By adhoc334 in forum Advanced JavaReplies: 5Last Post: 08-18-2010, 08:05 PM -
Paranthesis Matching???
By MuslimCoder in forum New To JavaReplies: 1Last Post: 10-27-2009, 06:02 AM -
matching and getting xml data
By Juuno in forum Advanced JavaReplies: 6Last Post: 04-26-2009, 06:25 PM -
Help with signature matching
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks