Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * Grouping symbols checking program / Homework 2
  6.  *
  7.  * @author Jose Ed. Dieck
  8.  * @version 1/30/2015
  9.  *
  10.  */
  11. public class Homework2 {
  12.  
  13.     private Homework2() {
  14.     }
  15.  
  16.     /**
  17.      * First spot on args data
  18.      */
  19.     public static final int FILEPATH = 0;
  20.  
  21.     /**
  22.      * Default value for an empty args length
  23.      */
  24.     public static final int EMPTY_ARGS = 0;
  25.  
  26.     /**
  27.      * Default count start variable
  28.      */
  29.     public static final int COUNT_START = 0;
  30.  
  31.     /**
  32.      * String variable containing opening grouping symbols for comparison
  33.      */
  34.     public static final String OPENING = "({[";
  35.  
  36.     /**
  37.      * String variable containing closing grouping symbols for comparison
  38.      */
  39.     public static final String CLOSING = ")}]";
  40.  
  41.     /**
  42.      * Main method for the program, taking in a file path as argument
  43.      *
  44.      * @param args
  45.      *            filepath for the file to be checked
  46.      * @throws FileNotFoundException
  47.      *             exception thrown if the file could not be found
  48.      */
  49.     public static void main(String[] args) throws FileNotFoundException {
  50.  
  51.         if (args.length == EMPTY_ARGS) {
  52.             System.out.println("Usage: java Homework2 (file path)");
  53.             return;
  54.         }
  55.  
  56.         File toRead = new File(args[FILEPATH]);
  57.         boolean checked = checkBraces(toRead);
  58.  
  59.         if (checked) {
  60.             System.out.println("The file is valid.");
  61.         } else {
  62.             System.out.println("The file is not valid.");
  63.         }
  64.  
  65.     }
  66.  
  67.     public static boolean checkBraces(File file) throws FileNotFoundException {
  68.  
  69.         Scanner read = new Scanner(file);
  70.         Stack<String> openChar = new Stack<String>();
  71.         String line = "";
  72.         String toCheck = "";
  73.  
  74.         while (read.hasNextLine()) {
  75.  
  76.             line = read.nextLine();
  77.  
  78.             for (int i = COUNT_START; i < line.length(); i++) {
  79.                 toCheck = Character.toString(line.charAt(i));
  80.                 if (OPENING.contains(toCheck)) {
  81.                     openChar.push(toCheck);
  82.                 } else if (CLOSING.contains(toCheck)) {
  83.                     if (openChar.isEmpty()) {
  84.                         read.close();
  85.                         return false;
  86.                     } else if (toCheck.equals(")")) {
  87.                         if (openChar.peek().equals("(")) {
  88.                             openChar.pop();
  89.                         } else {
  90.                             read.close();
  91.                             return false;
  92.                         }
  93.  
  94.                     } else if (toCheck.equals("}")) {
  95.                         if (openChar.peek().equals("{")) {
  96.                             openChar.pop();
  97.                         } else {
  98.                             read.close();
  99.                             return false;
  100.                         }
  101.                     } else {
  102.                         if (openChar.peek().equals("[")) {
  103.                             openChar.pop();
  104.                         } else {
  105.                             read.close();
  106.                             return false;
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.  
  113.         read.close();
  114.         return true;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement