Match grouping symbols A Java program contains various pairs

(Match grouping symbols) A Java program contains various pairs of grouping symbols, such as:
Parentheses: ( and )

Braces: { and }

Brackets: [ and ]

Note that the grouping symbols cannot overlap. For example, (a{b)} is illegal. Write a program to check whether a Java source-code file has correct pairs of grouping symbols. Pass the source-code file name as a command-line argument.

Solution

// GroupingSymbols.java
import java.io.*;
import java.util.*;

public class GroupingSymbols
{
   public static void main(String[] args) {

if (args.length != 1)
{
System.out.println(\"Usage: filename\");
System.exit(0);
}

File inputFile = new File(args[0]);
// Check if file exists
       if (!inputFile.exists()) {
           System.out.println(\"File does not exist!\");
           System.exit(1);
       }

       // Create a stack
Stack<String> charstack = new Stack<>();
// arraylist
ArrayList<String> symbolarray = new ArrayList<>();

// addinng all symbols to collection
Collections.addAll(symbolarray, \"(\", \")\", \"{\", \"}\", \"[\", \"]\");

try(BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))))
{
String str;
while ((str = bf.readLine()) != null)
{

for (char character : str.toCharArray())
{
String s = character + \"\";
int sIndex = symbolarray.indexOf(s);

if (sIndex == -1)
continue;

if (charstack.size() == 0)
{
charstack.push(s);
}
else
{
int lIndex = symbolarray.indexOf(charstack.peek());
if (sIndex - 1 == lIndex)
{
charstack.pop();
}

else
{
if ((lIndex & 1) == 1)
{
   System.out.println(\"ERROR at:\" + str);
}
charstack.push(s);
}
}

}

}
}
catch (FileNotFoundException exception)
{
System.out.println(\"File not found.\");
exception.printStackTrace();
}
catch (IOException exception)
{
System.out.println(\"IO error.\");
exception.printStackTrace();
}

}
}

(Match grouping symbols) A Java program contains various pairs of grouping symbols, such as: Parentheses: ( and ) Braces: { and } Brackets: [ and ] Note that th
(Match grouping symbols) A Java program contains various pairs of grouping symbols, such as: Parentheses: ( and ) Braces: { and } Brackets: [ and ] Note that th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site