Advertisement
brilliant_moves

CheckBalanced3.java

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