I really need help with this java program HW Write a program

I really need help with this java program HW.

Write a program to read in a text file (of your own making) and test for balanced parenthesis and balanced brackets. Call the file: “assn3data.txt”. You can use Notepad (Win) or Textedit (Mac) to create the file, to make it easier on yourself – do not add any blank lines to the file.

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 of the file for the balanced/unbalanced result.

Note: this program does not need to be recursive, but you can use recursion if you like.

Use a Queue data structure to hold and print out the results of the testing after all the lines in the file are processed. 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.

YOU MUST INSTANTIATE AN OBJECT OF YOUR OWN CLASS. NO MORE USING \"public static\" methods from this assignment on.

Make your output look like this example output:

File read in: <filename>

Results:

Line 1: ‘<display line here>’ Balanced

Line 2: ‘<display line here>’ Unbalanced

.

.

.

Line n: ‘<display line here>’ Balanced

Solution

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CheckBalanced {

    public ArrayList<String> readFileContent(File file) {
        ArrayList<String> content = new ArrayList<>();
        String s = null;
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            try {
                while ((s = br.readLine()) != null) {
                    content.add(s);
                }
            } catch (IOException ex) {
                Logger.getLogger(CheckBalanced.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CheckBalanced.class.getName()).log(Level.SEVERE, null, ex);
        }
        return content;

    }

    public PriorityQueue<String> validateContent(ArrayList<String> content) {
        PriorityQueue<String> priorityQueue = new PriorityQueue<String>();
        StringBuffer stringBuffer = null;
        int count = 1;
        for (String line : content) {
            stringBuffer = new StringBuffer();
            stringBuffer.append(\"Line \"+count++).append(\" : \").append(line + \" \").append(\" \" + checkContent(line));
            priorityQueue.add(stringBuffer.toString());
        }

        return priorityQueue;
    }

    public String checkContent(String sinput) {
        boolean flag = false;
        Stack<Character> input = new Stack<Character>();
        char[] c = new char[sinput.length()];
        c = sinput.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == \'{\' || c[i] == \'(\' || c[i] == \'[\') {
                input.push(c[i]);
            } else if (!input.empty()) {
                if (c[i] == \']\') {
                    if (input.pop() == \'[\') {
                        flag = true;
                        continue;
                    } else {
                        flag = false;
                        break;
                    }
                } else if (c[i] == \')\') {
                    if (input.pop() == \'(\') {
                        flag = true;
                        continue;
                    } else {
                        flag = false;
                        break;
                    }
                } else if (c[i] == \'}\') {
                    if (input.pop() == \'{\') {
                        flag = true;
                        continue;
                    } else {
                        flag = false;
                        break;
                    }
                }
            }
        }
        if (flag == true) {
            return \"Balanced\";
        } else {
            return \"UnBalanced\";
        }
    }

    public static void main(String[] args) {
        CheckBalanced checkBalanced = new CheckBalanced();
        ArrayList<String> content = checkBalanced.readFileContent(new File(\"\")); // provide your file path in File contructor
        PriorityQueue<String> priorityQueue = checkBalanced.validateContent(content);
        Iterator itr = priorityQueue.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

I really need help with this java program HW. Write a program to read in a text file (of your own making) and test for balanced parenthesis and balanced bracket
I really need help with this java program HW. Write a program to read in a text file (of your own making) and test for balanced parenthesis and balanced bracket
I really need help with this java program HW. Write a program to read in a text file (of your own making) and test for balanced parenthesis and balanced bracket

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site