Lab3 - } if(s.charAt(i) == ')'){ if(!(temp == '('))return...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 import java.util.* public class Lab3 { public static boolean isBalanced(String s){ Stack<Character> chars = new Stack<Character>() char temp for(int i = 0 i<s.length() i++){ if(s.charAt(i)=='['||s.charAt(i)=='{'||s.charAt(i)=='('){ chars.push(s.charAt(i)) } else if(s.charAt(i)==']'||s.charAt(i)=='}'||s.charAt(i)==')'){ if(chars.isEmpty())return false i temp = chars.pop() t if(s.charAt(i) == '}'){ if(!(temp == '{'))return false } if(s.charAt(i) == ']'){ if(!(temp == '['))return false
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } if(s.charAt(i) == ')'){ if(!(temp == '('))return false } } } if(chars.isEmpty())return true elsereturn false e } public static void main(String args){ p Scanner in = new Scanner(System.in) boolean keepGoing = true String inputStr = new String() S while(keepGoing){ System.out.println(&quot;Enter a string:&quot;) inputStr = in.next() System.out.println(isBalanced(inputStr)) System.out.println(&quot;Would you like to enter another String? (y or n):&quot;) S } } }...
View Full Document

Ask a homework question - tutors are online