Part B: A Java program contains various pairs of grouping symbols such as Parath
ID: 3541726 • Letter: P
Question
Part B:
A Java program contains various pairs of grouping symbols such as
Paratheses:( and )
Braces: { and }
Brackets:[ and ]
Single Quotes: ' and '
Double Quotes: " and "
< and >
Note that 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.
My Program:
import java.util.*;
import java.io.*;
public class SymbolMatch
{
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner(new File(args [0]));
Stack<Character> stack = new Stack<Character>();
int count = 0;
while(input.hasNext())
{
String line = input.nextLine();
count++;
System.out.println(count+" -> "+line);
for(int i = 0; i < line.length(); i++)
{
char c = line.charAt(i);
if(c == '"')
{
i += 1;
while(line.charAt(i) != '"')
i += 1;
continue;
}
if(c == ''')
{
i += 1;
continue;
}
switch(c)
{
case '{':
stack.push(c);
System.out.printf("Character { pushed in line %d ", count);
break;
case '(':
stack.push(c);
System.out.printf("Character ( pushed in line %d ", count);
break;
case '[':
stack.push(c);
System.out.printf("Character [ pushed in line %d ", count);
break;
case '<':
stack.push(c);
System.out.printf("Character < pushed in line %d ", count);
break;
case '}':
if(stack.peek() == '{')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
break;
case ')':
if(stack.peek() == '(')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case ']':
if(stack.peek() == '[')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case '"':
if(stack.peek() == '"')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case '>':
if(stack.peek() == '<')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
}
}
}
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
public class SymbolMatch
{
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a symbols : ");
String sentence = input.nextLine();
Stack<Character> stack = new Stack<Character>();
int count = 0;
while(input.hasNext())
{
String line = input.nextLine();
count++;
System.out.println(count+" -> "+line);
for(int i = 0; i < line.length(); i++)
{
char c = line.charAt(i);
if(c == '"')
{
i += 1;
while(line.charAt(i) != '"')
i += 1;
continue;
}
if(c == ''')
{
i += 1;
continue;
}
switch(c)
{
case '{':
stack.push(c);
System.out.printf("Character { pushed in line %d ", count);
break;
case '(':
stack.push(c);
System.out.printf("Character ( pushed in line %d ", count);
break;
case '[':
stack.push(c);
System.out.printf("Character [ pushed in line %d ", count);
break;
case '<':
stack.push(c);
System.out.printf("Character < pushed in line %d ", count);
break;
case '}':
if(stack.peek() == '{')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
break;
case ')':
if(stack.peek() == '(')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case ']':
if(stack.peek() == '[')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case '"':
if(stack.peek() == '"')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
case '>':
if(stack.peek() == '<')
stack.pop();
else
{
System.out.println("Improper nesting detected");
System.exit(1);
}
}
}
}
}
}