Write a program that reads an input text file consisting of a list of arithmetic
ID: 3554122 • Letter: W
Question
Write a program that reads an input text file consisting of a list of arithmetic expressions, one expression on each line. Each arithmetic expression may consist of three kinds of brackets, namely, ( ), [ ] and { }. There could either be spaces or no space between the brackets and arithmetic operators and variables.
The program should read each line and check if it is balanced with respect to the three symbols. For this, you should use a stack. The program should report whether each line is correct or incorrect. The output for the above example would be (printed on the standard output, not to a file).
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BracketMatch {
/**
* @param args
*/
public static void main(String[] args) {
BracketMatch match = new BracketMatch();
File f = new File("src/com/tcs/citi/test/basic/input.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
boolean result = match.isBalanced(line);
if(result)
System.out.println("Expression :"+line+" is matched");
else
System.out.println("Expression :"+line+" is not matched");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isBalanced(String str){
if(true){
return true;
}else{
return false;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.