I need help with this assignment Write a program to read in
I need help with this assignment.
Write a program to read in a file and test for balanced parenthesis and balanced brackets.
Example of balanced parenthesis and brackets: ((x-y) + [n-1] / z )
Example of unbalanced parenthesis and brackets: ( a + b [ z + 1 ] ( z / 2)
The file can contain multiple lines, with each line having parenthesis and/or brackets. Each line will be self-contained, so you do not need to worry about matching parenthesis and brackets across multiple lines in the file. If a line does not contain any ( or [ characters at all, consider it balanced. You should use a stack data structure to test each line for the balanced/unbalanced result. Use a Queue data structure to hold and print out the results of the testing. The Queue should hold the line number for each line of the file which was read in and the result (balanced or unbalanced) for each line number in the file. See example output below. Your program should use a class, and have an object of that class which you instantiate and use. You should call methods to do most of the work in the program. Also, comment your code where appropriate.
Every line does not need to be commented, but you should have a proper header, and each method should have some comments as well as a major section such as a loop, conditional statement or an abstract data type.
Example output: File read in: Results: Line 1: Balanced Line 2: Unbalanced . . . Line n: Balanced
Solution
Main.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String args[]) throws IOException{
System.out.println(\"Enter File Path\");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pathToFile = br.readLine();
//Try to create Input Stream from file if it exists, otherwise exception is thrown
try(InputStream is = new FileInputStream(pathToFile)){
String line;
br = new BufferedReader(new InputStreamReader(is));
ArrayList<String> lines = new ArrayList<String>();
// Read lines from file and store in a list
while((line = br.readLine())!= null){
lines.add(line);
}
is.close();
// Create a new Object of type CheckParanthesis, which takes an ArrayList as a parameter
CheckParanthesis cp = new CheckParanthesis(lines);
//Call the check function, which returns a list of string with the line number and result
ArrayList<String> result = cp.check();
//Print out the result, or do whatever with it
for(String s: result){
System.out.println(s);
}
}
catch(FileNotFoundException e){
System.out.println(\"File Not Found\");
}
}
}
CheckParanthesis.java
import java.util.ArrayList;
import java.util.Stack;
public class CheckParanthesis {
private Stack<Character> s;
private ArrayList<String> lines;
CheckParanthesis(ArrayList<String> lines){
this.lines = lines;
//Initialize the stack
s = new Stack<Character>();
}
//isBalanced returns true if a line is balanced and false otherwise
boolean isBalanced(String line){
if(line.isEmpty()){
return true;
}
/*
* The loop works in this way
* It is iterated over the complete length of the string
* An opening bracket is pushed onto the stack whenever it is encountered
* Only opening brackets are pushed, all other characters are ignored
* Whenever a closing bracket is encountered, the top character of the stack is popped if the bracket is of the same type
* Otherwise the function returns false since the brackets are not balanced
* Example, Top of stack = \'{\' and encountered \']\', which is wrong
* Hence unbalanced.
*/
for(int i = 0; i<line.length();i++){
char currentChar = line.charAt(i);
if(currentChar == \'{\' || currentChar == \'(\' || currentChar == \'[\'){
s.push(currentChar);
}
else if(currentChar == \'}\'){
if(s.peek() == \'{\'){
s.pop();
}
else{
return false;
}
}
else if(currentChar == \']\'){
if(s.peek() == \'[\'){
s.pop();
}
else{
return false;
}
}
else if(currentChar == \')\'){
if(s.peek() == \'(\'){
s.pop();
}
else{
return false;
}
}
}
if(!s.isEmpty()){
return false;
}
return true;
}
/*
* The check function calls the isBalanced function with each line of the file
* It stores the line number and the result in a list, and returns it after completion
*/
ArrayList<String> check(){
ArrayList<String> result = new ArrayList<String>();
int i = 1;
for(String line : lines){
if(isBalanced(line)){
result.add(\"Line \"+ i + \": Balanced\");
}
else{
result.add(\"Line \"+ i + \": Unbalanced\");
}
i++;
}
return result;
}
}


