Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write this in java One of the most useful real world implementations of S

ID: 3836127 • Letter: P

Question

Please write this in java

One of the most useful real world implementations of Stacks is with language compilers. We are going to implement a very simple version of a compiler using recursion and the Stack class. As we know, the Stack class has a push and pop. Stack is a built-in java.util class library. You can create an instance like this:

static Stack<Character> myStack = new Stack<Character>();

Then, you can access its methods just as like any other class.

In this program, you are going to read in any XML file and check to see if it is valid based on its < and /> brackets. If an open angle bracket "< " is found, you should push it onto the stack, and when you find a closed angle bracket />, you are to pop off the stack. At the end of the program, you should print out whether the file is valid (i.e. the stack is empty) or if invalid (the stack is not empty). You are to read the file in recursively and for each line in the file you are to look recursively through the line and push and pop as necessary.

Specifics:

You should use the built in Stack class with its push and pop methods

You are to read recursively in a java file.

You are to read recursively in each line and push if a < open angle bracket is found and pop if a

/> closed angle bracket is found.

You should have an appropriate exception handler for the pop.

After the recursive calls, you should display whether the file is valid or not.

Sample XML

<note>
<to>

Tove </to>

<from>
Jani

</from> <heading>

Reminder </heading>

<body>
Don't forget me this weekend! </body>

</note>

Explanation / Answer

In order to solve the above problem. I have written the following method which accepts string as the argument and checks whether the entered xml is valid or not. It accepts string as the argument and returns a boolean value to signify the validity of the xml. I have not written the whole program because this method will give you independence to take the input from multiple sources parse them in string and provide as an input to this method. Hope this helps. Cheers! public static boolean isXMLValid(String s) { HashMap map = new HashMap(); map.put('