Lab3 - if(s.charAt(i == if(temp ==)return false...

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( boolean keepGoing = true String inputStr = new String() S while(keepGoing){ System.out.println("Enter a string:") inputStr = System.out.println(isBalanced(inputStr)) System.out.println("Would you like to enter another String? (y or n):") S } } }...
View Full Document

This lab report was uploaded on 04/21/2008 for the course CSC 103 taught by Professor Franklin during the Spring '07 term at Cal Poly.

Ask a homework question - tutors are online