Java expert please help me the JAVA program below? please create your own code.
ID: 3828060 • Letter: J
Question
Java expert please help me the JAVA program below?
please create your own code. DON'T copy any code from anywhere, that's why I am asking for help here.
(Match grouping symbols) A Java program contains various pairs of grouping symbols, such as: L Parentheses C and D L Braces and H L Brackets C and Note that the grouping symbols cannot overlap. For example, Ca{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.Explanation / Answer
// GroupingSymbols.java
import java.io.*;
import java.util.*;
public class GroupingSymbols
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("Enter filename in while running the program");
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("ILLEGAL");
}
charstack.push(s);
}
}
}
}
}
catch (FileNotFoundException exception)
{
System.out.println("File not found.");
exception.printStackTrace();
}
catch (IOException exception)
{
System.out.println("Input/Output error.");
exception.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.