Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.LinkedList;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class CheckBalanced3 {
- /**
- * Program: CheckBalanced3.java
- * Purpose: Read a Java source code file and check it has balanced brackets {}()[].
- * Do not include brackets within quotes or comments.
- * Version: More efficient - reads file once, uses arraylist.
- * Creator: Chris Clarke
- * Created: 13.11.2015
- */
- // create array list
- private static ArrayList<String> lines = new ArrayList<String>();
- // create linked list object as instance variable
- LinkedList<Integer> list = new LinkedList<Integer>();
- // initialise top (of stack)
- static int top = -1;
- public void push(int t) {
- list.add(new Integer(t));
- } // push()
- public int pop() {
- if (top != -1)
- return list.remove(top);
- return top;
- } // pop()
- public static String doBalanceCheck(char ch0, char ch1, CheckBalanced3 stack, String javaSource,
- boolean readFile) {
- File f;
- Scanner scan = null;
- String line;
- String result = "";
- char ch;
- int lineNum = 0;
- int openLineNum = 0;
- int position = 0;
- top = -1;
- boolean inSingleQuotes = false;
- boolean inDoubleQuotes = false;
- boolean inMultiLineComment = false;
- if (readFile) {
- try {
- f = new File(javaSource);
- scan = new Scanner(f);
- } catch (IOException e) {
- System.out.println("Caught IOException");
- System.exit(1);
- } // try
- while (scan.hasNextLine()) {
- line = scan.nextLine();
- lines.add(line);
- } // while
- if (scan!=null) scan.close();
- } // if
- for (String ln: lines) {
- for (int i=0; i<ln.length(); i++) {
- ch = ln.charAt(i);
- if (ch=='\"') {
- inDoubleQuotes = !inDoubleQuotes;
- } else if (ch=='\'') {
- inSingleQuotes = !inSingleQuotes;
- } else if (ch=='/' && i<ln.length()-1) {
- if (ln.charAt(i+1)=='/') {
- break; // single line comment - ignore rest of line
- } else if (ln.charAt(i+1)=='*') {
- inMultiLineComment = true;
- } // if ln
- } else if (ch=='*' && i<ln.length()-1) {
- if (ln.charAt(i+1)=='/') {
- inMultiLineComment = false;
- } // if ln
- } // if ch
- if (!inDoubleQuotes && !inSingleQuotes && !inMultiLineComment) {
- if (ch==ch0) {
- stack.push(++top);
- openLineNum = lineNum;
- position = i;
- } // if ch
- if (ch==ch1) {
- stack.pop();
- top--;
- } // if ch
- } // if !in
- } // for i
- lineNum++;
- } // for ln
- if (top == -1)
- result = ""+ch0+ch1+": BALANCED";
- else
- result = ""+ch0+ch1+": NOT BALANCED at line"
- +(openLineNum+1)+", position "+(position+1);
- return result;
- } // doBalanceCheck()
- public static void main(String[] args) {
- CheckBalanced3 myStack = new CheckBalanced3();
- if (args.length==0) {
- System.out.println("Usage: java CheckBalanced3 \"MyProgram.java\"");
- } else {
- System.out.println (doBalanceCheck('{', '}', myStack, args[0], true));
- System.out.println (doBalanceCheck('(', ')', myStack, args[0], false));
- System.out.println (doBalanceCheck('[', ']', myStack, args[0], false));
- } // if
- } // main()
- } // class CheckBalanced3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement