Check11D - { chars.add(i.charAt(0)); } else if...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.io.*; import java.util.*; import java.nio.BufferOverflowException; import type.lib.CharStack; i public class Check11D { public static void main(String[] args) { PrintStream out = System.out; out.println("Enter expression"); String buffer = new Scanner(System.in).nextLine(); final String REGEX = "[\\Q[]{}()\\E]",//[]{}() OPEN = "[\\Q[{(\\E]", //[{( CLOSE = "[\\Q]})\\E]"; //]}) try { CharStack chars = new CharStack(); for (String i : buffer.split("")) { if (i.matches(OPEN))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { chars.add(i.charAt(0)); } else if (i.matches(CLOSE)) { if (!chars.isEmpty()) { if (chars.removeLast() != REGEX.charAt(REGEX.indexOf(i) - 1)) { throw new Exception("Overlapping"); } } else if (chars.isEmpty()) { throw new Exception("Imbalanced"); } } } throw new Exception((!chars.isEmpty()) ? "Imbalanced" : "OK"); } catch (BufferOverflowException e) { out.println("Too Deeply Nested"); } catch (Exception e) { out.println(e.getMessage()); } } }...
View Full Document

Ask a homework question - tutors are online