Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class ParenthesisChecker {
- public static void main(String[] args) throws IOException {
- File file = new File("C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\practice\\Test.txt");
- try {
- BufferedReader br = new BufferedReader(new FileReader(file));
- Stack st = new StackLinked();
- String data = "";
- String msg = "";//"un-balanced";
- while((data = br.readLine()) != null) {
- System.out.print(data + " ");
- char[] ch = data.toCharArray();
- //Traverse array
- for(int i = 0; i < ch.length; i++) {
- char c = ch[i];
- char b='\u0000';
- //If the current character is starting bracket, then push them in a stack
- if(c == '(' || c == '{' || c == '[' || c == '<') {
- st.push(c);
- } else if(c == ')' || c == '}' || c == ']' || c == '>') {
- if (!st.isEmpty()) {
- b = (char) st.peek();//.toString().charAt(0);
- if (b == '(' || b == '{' || b == '[' || b == '<') {
- st.pop();
- } else {
- st.push(c);
- }
- }
- }
- if (st.isEmpty()) {
- msg = "balanced";
- } else {
- msg = "unbalanced";
- }
- }
- System.out.println(msg);
- }
- br.close();
- }catch(FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement