Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Dacks the stack in order to wack the crack.
- *
- * @author Ulizio
- * @version 1.0
- */
- //Imports
- import java.io.*;
- import java.util.Scanner;
- import java.util.*;
- public class StackerDacker
- {
- public static void main (String [] args) {
- /*
- * Entire program works very well, except, when the stack tries to pop and there is nothing there
- * rather than return null it throws a null pointer exception.
- *
- * When run with too many opening parentheses: Success
- * When run with correct number of parentheses: Null Pointer Exception
- * When run with too many closing parentheses: Null Pointer Exception
- *
- * Conclusion: Stack may be null
- */
- try {
- Scanner scan = new Scanner(new BufferedReader(new FileReader("file.txt")));
- String input = scan.nextLine();
- int length = input.length();
- Stack<Integer> x = new Stack<Integer>();
- int i;
- for(i = 0; i<length; i++) {
- String temp = input.substring(i,i+1);
- if(temp.equals("(")) {
- x.push(1);
- }
- if(temp.equals(")")) {
- if (x.pop().equals(null)) {
- System.out.println("There appear to be too many closing parentheses.");
- }
- else {
- x.pop();
- }
- }
- else{
- }
- }
- if (x.peek() != (null)) {
- System.out.println("There appear to be too many opening parentheses.");
- }
- else {
- System.out.println("This code appears to check out.");
- }
- }
- catch(Exception e) {
- System.out.println("Critical: " + e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement