Advertisement
NicholasCSW

StackerDacker

Feb 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. /**
  2.  * Dacks the stack in order to wack the crack.
  3.  *
  4.  * @author Ulizio
  5.  * @version 1.0
  6.  */
  7. //Imports
  8. import java.io.*;
  9. import java.util.Scanner;
  10. import java.util.*;
  11. public class StackerDacker
  12. {
  13.     public static void main (String [] args) {
  14.        
  15.         /*
  16.          * Entire program works very well, except, when the stack tries to pop and there is nothing there
  17.          * rather than return null it throws a null pointer exception.
  18.          *
  19.          * When run with too many opening parentheses: Success
  20.          * When run with correct number of parentheses: Null Pointer Exception
  21.          * When run with too many closing parentheses: Null Pointer Exception
  22.          *
  23.          * Conclusion: Stack may be null
  24.          */
  25.        
  26.         try {
  27.             Scanner scan = new Scanner(new BufferedReader(new FileReader("file.txt")));
  28.             String input = scan.nextLine();
  29.             int length = input.length();
  30.            
  31.             Stack<Integer> x = new Stack<Integer>();
  32.            
  33.             int i;
  34.            
  35.             for(i = 0; i<length; i++) {
  36.                 String temp = input.substring(i,i+1);
  37.                 if(temp.equals("(")) {
  38.                     x.push(1);
  39.                 }
  40.                 if(temp.equals(")")) {
  41.                     if (x.pop().equals(null)) {
  42.                         System.out.println("There appear to be too many closing parentheses.");
  43.                     }
  44.                     else {
  45.                         x.pop();
  46.                     }
  47.                 }
  48.                 else{
  49.                    
  50.                 }
  51.             }
  52.            
  53.             if (x.peek() != (null)) {
  54.                 System.out.println("There appear to be too many opening parentheses.");
  55.             }
  56.             else {
  57.                 System.out.println("This code appears to check out.");
  58.             }
  59.            
  60.         }
  61.         catch(Exception e) {
  62.             System.out.println("Critical: " + e);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement