1. Many encoded strings contain delimiters. A delimiter is a non-empty string that acts as a boundary between different parts of a larger string. The delimiters involved in this question occur in pairs that must be balanced, with each pair having an open delimiter and a close delimiter. There will be only one type of delimiter for each string.
public class Delimiters
{
 /** The open and close delimiters. */
 private String openDel;
 private String closeDel;
 /** Constructs a Delimiters object where open is the open delimiter and close is the
 * close delimiter.
 * Precondition: open and close are non-empty strings.
 */
 public Delimiters(String open, String close)
 {
 openDel = open;
 closeDel = close;
 }
 /** Returns an ArrayList of delimiters from the array tokens, as described in part (a). */
 public ArrayList<String> getDelimitersList(String[] tokens)
 { /* to be implemented in part (a) */ }
 /** Returns true if the delimiters are balanced and false otherwise, as described in part (b).
 * Precondition: delimiters contains only valid open and close delimiters.
 */
 public boolean isBalanced(ArrayList<String> delimiters)
 { /* to be implemented in part (b) */ }
 // There may be instance variables, constructors, and methods that are not shown.
}

(a) A string containing text and possibly delimiters has been split into tokens and stored in String[] tokens. Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter. You will write the method getDelimitersList, which returns an ArrayList containing all the open and close delimiters found in tokens in their original order.

public ArrayList<String> getDelimitersList(String[] tokens)  //string with the getDelimitersList method
{

    ArrayList<String> d = new ArrayList<String>();

    for (String str : tokens)   // for all of the tokens
    {
        if (str.equals(openDel) || str.equals(closeDel))    // if the token is either an open or close delimiter
        {
            d.add(str);
        }
    }
    return d;  // in the event that the substring is not a delimiter

}

Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise, they are not balanced.

  1. When traversing the ArrayList from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.

  2. The total number of open delimiters is equal to the total number of close delimiters. Consider a Delimiters object for which openDel is "" and closeDel is "".

public boolean isBalanced(ArrayList<String> delimiters)  // method of isBalanced
{
    int openCount = 0;    // initializing both openedCount and closedCount variables at 0
    int closeCount = 0;

    for (String str : delimiters)
    {
        if (str.equals(openDel))
        {
            openCount++;
        }
        else
        {
            closeCount++;
        }
        if (closeCount > openCount)   // making sure there isn't a point there are more closed than opeend delimiters
        {
            return false;
        }
 }

 if (openCount == closeCount)
 {
    return true;    // statement returns true when closed and opened delimiters hold the same value
 }
 else
 {
    return false;
 }
}