I been working on this everyday and im stuck trying to make my brackets work where when user add () it shows its balance. But i am missing other brackets such as {}, and []. I wanted to know how to fix the method where program when user type (), [], {}, the program will display it being balance.
HTML Code:objective: program that checks the parentheses(), brackets [], and braces {} for balance and proper nesting in an input string. The string can have nested brackets at any level, as well as any other "non-important" characters. With respect to your implementation, you must use Java's Stack class.[HTML] import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Queue; import java.util.LinkedList; public class QueueSample extends JFrame { JButton oneButton = new JButton("Push"); JButton twoButton = new JButton("Pop"); JButton peekButton = new JButton("Peek"); JButton evaluateButton = new JButton ("Evaluate"); private Queue <String> theQueue = new LinkedList <String> (); public static void main() { QueueSample demo = new QueueSample(); demo.setSize(400,100); demo.setDefaultCloseOperation(EXIT_ON_CLOSE); demo.setTitle("Queue Demo"); demo.createGUI(); demo.setVisible(true); } public void createGUI() { Container window = getContentPane(); window.setLayout(new FlowLayout()); oneButton.addActionListener(new EnqueueAction()); window.add(oneButton); twoButton.addActionListener(new DequeueAction()); window.add(twoButton); peekButton.addActionListener(new peekAction()); window.add(peekButton); window.add(evaluateButton); evaluateButton.addActionListener(new evaluateAction()); } private class evaluateAction implements ActionListener{ public void actionPerformed (ActionEvent e) { Test myTest = new Test(); myTest.checkExpression(); } } private class EnqueueAction implements ActionListener { public void actionPerformed (ActionEvent e) { String s = JOptionPane.showInputDialog("Enter a value to enqueue"); theQueue.offer(s); } } private class DequeueAction implements ActionListener { public void actionPerformed (ActionEvent e) { try{ JOptionPane.showMessageDialog (null, "Removed: " + theQueue.remove()); } catch (Exception event){ JOptionPane.showMessageDialog(null, "Stack is empty", "Error", JOptionPane.ERROR_MESSAGE); } } } //make a button and associate it with check syntax action //display a dialog box //Jlabels private class peekAction implements ActionListener{ public void actionPerformed (ActionEvent e){ try{ JOptionPane.showMessageDialog(null, "The top is: " + theQueue.peek()); } catch (Exception event){ JOptionPane.showMessageDialog(null, "Stack is empty", "Error", JOptionPane.ERROR_MESSAGE); } } } } [/HTML]
[HTML] public class BalancedParentheses{ public static void main(){ System.out.println("Enter set of parentheses: "); Scanner s = new Scanner(System.in); String input = s.nextLine(); if(input.length() != 0 && check(input)){ checkBalance(input); }else System.out.println("Incorrect Input, Try Again !"); } public static boolean check(String a){ boolean isReturn = true; for(int i = 0; i < a.length(); i++){ if(a.charAt(i) != '(' && a.charAt(i) != ')'){ isReturn = false; break; } } return isReturn; } public static boolean look(String b){ boolean isReturn = true; for(int i = 0; i < b.length(); i++){ if(b.charAt(i) != '[' && b.charAt(i) != ']'){ isReturn = false; break; } } return isReturn; } public static void checkBalance(String input){ Stack<Character> st = new Stack<Character>(); int index = 0; int[] arr = new int[input.length()]; for(int i = 0; i < input.length(); i++){ if(input.charAt(i) == '('){ st.push(input.charAt(i)); arr[index] = i; index++; }else{ if(input.charAt(i) == ')' && !st.empty() && st.peek().equals('(')){ st.pop(); index--; input = input.substring(0, arr[index]) + "(" + input.substring((arr[index] + 1), i) + ")" + input.substring(i+1, input.length()); }else st.push(input.charAt(i)); } } System.out.println("Result: )" ); } public static void anotherBalance(String input){ Stack<Character> st = new Stack<Character>(); int index = 0; int [] arr = new int[input.length()]; for(int i = 0; i < input.length(); i++){ if(input.charAt(i) == '['){ st.push(input.charAt(i)); arr[index] = i; index++; }else{ if(input.charAt(i) == ']' && !st.empty() && st.peek().equals(']')){ st.pop(); index--; input = input.substring(0, arr[index]) + "[" + input.substring((arr[index] + 1), i) + "]" + input.substring(i+1, input.length()); }else st.push(input.charAt(i)); } } if(st.empty()) // String input = JOptionPane.showInputDialog(null, "The brackets are well balanced!"); System.out.println("The brackets are well balanced !"); else System.out.println("The brackets are not well balanced !"); } [/HTML] }


LinkBack URL
About LinkBacks
Reply With Quote