CSci 2101 Lab 2. Stacks.

Due Thursday, May 27th at 11:59pm (by e-mail)

30 points

The lab is done in pairs.

Using a stack to check for matching parentheses

Implement the program according to the comments in the strating code (see below). Your program will be graded based on correctness of the results, clear documentation (if some cases are not handled properly, document them), code quality, and on the quality of error messages for invalid strings.

You might find the break statement useful. It allows you to break out of a loop, i.e. stop the loop execution.

import java.util.Scanner;
import java.util.Stack;

public class Lab2Stack {

	 * A solution for the matching parentheses problem.
	 * A given string consists of characters (without white spaces) and 
	 * may have any number of parentheses and curly braces. The program 
	 * checks if all parentheses and braces form matching 
	 * non-overlapping pairs. There may be any symbols in-between 
	 * parentheses and/or braces. 
         * For instance, the following two 
	 * strings are valid matching sequences: 
	 * ab(c{}de(fghi))jk
	 * (a(b)c)d{e(f)}g
	 * The following sequences are not valid:
	 * ab(c{de(fghi))jk (missing a closing curly brace)
	 * (a(b))c)d{e(f)}g (an extra closing parenthesis after c)
	 * (a{b)c} (overlapping parentheses and braces)
	 * }{ (a closing brace before an opening one)
	 * a(b(c) (missing a closing parenthesis)
	 * The program prints the given string and the word "valid"
	 * or "invalid". If the string is invalid, additionally a brief 
	 * message is printed indicating what the problem is.
	public static void main(String[] args) {
		Stack<Character> symbols = new Stack<Character>();
		// reading input. Assumption: no whitespaces
		Scanner read = new Scanner(;
		System.out.println("Please enter your input string");
		String input =; // reads until a whitespace or new line
		// checking the input/output
		// this variable might be helpful
		boolean isValid = true;



How to submit

Submit the java file(s) by e-mail to me. The subject of the message must be 2101 Lab 2. Make sure to CC your group partner.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.