Advertisement
brilliant_moves

CheckBalanced2.java

Nov 13th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.71 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class CheckBalanced2 {
  6.  
  7.     /**
  8.     *   Program: CheckBalanced2.java
  9.     *   Purpose: Read a Java source code file and check it has balanced brackets {}()[].
  10.     *       Do not include brackets within quotes or comments.
  11.     *   Creator: Chris Clarke
  12.     *   Created: 13.11.2015
  13.     */ 
  14.  
  15.     // create linked list object as instance variable
  16.     LinkedList<Integer> list = new LinkedList<Integer>();
  17.  
  18.     // initialise top (of stack)
  19.     static int top = -1;
  20.  
  21.     public void push(int t) {
  22.         list.add(new Integer(t));
  23.     } // push()
  24.  
  25.     public int pop() {
  26.         if (top != -1)
  27.             return list.remove(top);
  28.         return top;
  29.     } // pop()
  30.  
  31.     public static void doBalanceCheck(char ch0, char ch1, CheckBalanced2 stack, String javaSource) {
  32.         File f;
  33.         Scanner scan = null;
  34.         String line;
  35.         char ch;
  36.         int lineNum = 0;
  37.         int openLineNum = 0;
  38.         int position = 0;
  39.         top = -1;
  40.         boolean inSingleQuotes = false;
  41.         boolean inDoubleQuotes = false;
  42.         boolean inMultiLineComment = false;
  43.  
  44.         System.out.printf("%c %c: ", ch0, ch1);
  45.  
  46.         try {
  47.             f = new File(javaSource);
  48.             scan = new Scanner(f);
  49.         } catch (IOException e) {
  50.             System.out.println("Caught IOException");
  51.             return;
  52.         } // try
  53.  
  54.         while (scan.hasNextLine()) {
  55.             line = scan.nextLine();
  56.             for (int i=0; i<line.length(); i++) {
  57.                 ch = line.charAt(i);
  58.                 if (ch=='\"') {
  59.                     inDoubleQuotes = !inDoubleQuotes;
  60.                 } else if (ch=='\'') {
  61.                     inSingleQuotes = !inSingleQuotes;
  62.                 } else if (ch=='/' && i<line.length()-1) {
  63.                     if (line.charAt(i+1)=='/') {
  64.                         break; // ignore rest of line
  65.                     } else if (line.charAt(i+1)=='*') {
  66.                         inMultiLineComment = true;
  67.                     }
  68.                 } else if (ch=='*' && i<line.length()-1) {
  69.                     if (line.charAt(i+1)=='/') {
  70.                         inMultiLineComment = false;
  71.                     }
  72.                 }
  73.                 if (!inDoubleQuotes && !inSingleQuotes && !inMultiLineComment) {
  74.                     if (ch==ch0) {
  75.                         stack.push(++top);
  76.                         openLineNum = lineNum;
  77.                         position = i;
  78.                     }
  79.                     if (ch==ch1) {
  80.                         stack.pop();
  81.                         top--;
  82.                     }
  83.                 } // if
  84.             } // for
  85.             lineNum++;
  86.         } // while
  87.  
  88.         if (scan!=null) scan.close();
  89.  
  90.         if (top == -1) System.out.printf("BALANCED%n");
  91.         else System.out.printf("NOT BALANCED at line %d, position %d%n", openLineNum+1, position+1);
  92.     } // doBalanceCheck()
  93.  
  94.     public static void main(String[] args) {
  95.         CheckBalanced2 myStack = new CheckBalanced2();
  96.         if (args.length==0) {
  97.             System.out.println("Usage: java CheckBalanced2 \"MyProgram.java\"");
  98.         } else {
  99.             myStack.doBalanceCheck('{', '}', myStack, args[0]);
  100.             myStack.doBalanceCheck('(', ')', myStack, args[0]);
  101.             myStack.doBalanceCheck('[', ']', myStack, args[0]);
  102.         } // if
  103.     } // main()
  104.  
  105. } // class CheckBalanced2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement