write a program name match.java.... I need the whole code You are to write a pro
ID: 3544381 • Letter: W
Question
write a program name match.java.... I need the whole code
You are to write a program name match. Java that reads input from a Java program file, check to see if the comments (//. /*....*/), parenthesis 0, square brackets [] if any. and curly braces {} are correctly used in the program. If there is any error, print out a message saying what the error is and which line that error occurred. The program will prompt the user for the input file name from which it must read the input. The program should read the characters one at a time. If it encounters a / or a [ or a (or a {, it should push it in their respective stack. Then if it encounters a closing character, it must go to the respective stack, pop and match. If there is no match, then print a message saying that there is no OPENING corresponding character and then print the line where the closing character occurred. More would be explained in class.Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class CollegeStudent {
private Stack<Character> symbolStack;
public void balance(String inputText) {
symbolStack = new Stack<Character>();
for (int index = 0; index < inputText.length(); index++) {
char currentSymbol = inputText.charAt(index);
switch (currentSymbol) {
case '(':
case '[':
case '{':
symbolStack.push(currentSymbol);
break;
case ')':
case ']':
case '}':
if (!symbolStack.isEmpty()) {
char symbolStackTop = symbolStack.pop();
if ((currentSymbol == '}' && symbolStackTop != '{')
|| (currentSymbol == ')' && symbolStackTop != '(')
|| (currentSymbol == ']' && symbolStackTop != '[')) {
System.out.println("Unmatched closing bracket while parsing " + currentSymbol + " at " + index+1);
return;
}
} else {
System.out.println("Extra closing bracket while parsing " + currentSymbol + " at character " + index+1);
return;
}
break;
default:
break;
}
}
if (!symbolStack.isEmpty())
System.out.println("Insufficient closing brackets after parsing the entire input text");
else
System.out.println("Brackets are balanced");
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("c://input.txt"));
String input = null;
StringBuilder sb = new StringBuilder();
while ((input = in.readLine()) != null) {
sb.append(input);
}
new CollegeStudent().balance(sb.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.