Question: having issues with this piece of code for a programming project, when I test the program it does not correctly process the test files. Mainly

having issues with this piece of code for a programming project, when I test the program it does not correctly process the test files. Mainly for "Unmatch" and "mismatch" but it also puts the wrong output for all almost all tests. delimiters are not properly encountered. It won't allow me to send the full code but here is the portion where I think the issue is.
Stack code :
Stack delimiterStack = new Stack<>();
Character ch;
boolean mismatchFound = false;
// Process each character in the file
while ((ch = checker.nextChar())!= null && !mismatchFound){
switch (ch){
case '(':
case '[':
case '{':
delimiterStack.push(ch); // Push opening delimiters onto the stack
break;
case ')':
case ']':
case '}':
if (delimiterStack.isEmpty()){
System.out.printf("Unmatched closing delimiter '%c' at %s%n", ch, checker.getPosition());
mismatchFound = true;
} else {
char openDelimiter = delimiterStack.pop();
if (!isMatchingPair(openDelimiter, ch)){// Mismatched delimiters
System.out.printf("Mismatched delimiter '%c' at %s%n", ch, checker.getPosition());
mismatchFound = true;
}
}
break;
}
}
if (!mismatchFound && !delimiterStack.isEmpty()){
char unmatchedOpenDelimiter = delimiterStack.pop();
System.out.printf("Unmatched opening delimiter '%c' at the end of the file%n", unmatchedOpenDelimiter);
} else if (!mismatchFound){
System.out.println("All delimiters are properly matched.");
}
try {
checker.close();
} catch (IOException e){
System.out.println("Error closing the file.");
}
scanner.close();
}
this is the requirement:
The second class should contain the main method. It should read in the file name from the keyboard until a valid file name is entered and then create an object of the first class. It should then repeatedly call the method that returns the next character until it returns a null character indicating the end of the file or until a mismatch of delimiters is encountered. If the character is a left delimiter it should be pushed onto a delimiter stack. If it is a right delimiter, the stack should be popped and a check should be made to ensure that the delimiters are of a matching type. If the delimiters do not match, a message should be displayed indicating what delimiter was encountered and at what position. You may use the defined Java Stack class.
Here is test coded for unmatched and another:
public class TestCase2{
public static void main(String[] args){
int x =10;
if (x >5)){
System.out.println("x is greater than 5");
}
}
}
public class TestCase5{
public static void main(String[] args){
// This is a comment with (unmatched parenthesis
String s = "This is a string with [brackets]{braces}";
/* This is a multi-line comment
with {unmatched}[delimiters */
System.out.println("Everything is fine");
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!