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

Write a program to input a textfile, and, using a stack, process the entire file

ID: 3641650 • Letter: W

Question

Write a program to input a textfile, and, using a stack, process the entire file, looking for parentheses and curly braces, then to output whether the parentheses and curly braces are properly nested. Any characters other than parentheses and curly braces should be ignored. You can use TextFileReader.java, documented as in these JavaDoc pages, to input a text file from the same folder as the program file. You can use the Stack<E> class from java.util.

Note that a good text file to try your program out on is a .java file. Also note that .java files with parentheses or curly braces in comments or quotes may actually not be properly nested even if they compile.

To insure proper Text input, be sure to add a blank line at the end of the input file.


Here is TextFileReader.java :

import java.io.*;
import java.util.*;
/**
TextFileReader is an Iterable class that can be used to iterate through
a text file, one Character at a time.
Use it as in the following example:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a filename: ");
String fname=sc.nextLine();
TextFileReader f = new TextFileReader(fname);
for (Character ch:f) System.out.print(ch);
}
@author Bruce Martin
*/
public class TextFileReader implements Iterable<Character> {

/** textBuffer is an array of Character elements from the text file. */
private Character[] textBuffer;
/** size is the number of Characters in textBuffer */
int size;
/** A one-String-parameter constructor opens the file represented by the String,
from the same directory as the program file, and reads the contents into
textBuffer.
@param fname is the name of the text file.
*/
public TextFileReader(String fname) {
textBuffer = new Character[100];
readFromFile(fname);
}
/** ensureCapacity doubles the capacity of the Character array and copies the old
content to it, replacing the textBuffer array, if the current array is less than
the new capacity needed.
@param newCapacity is the capacity requested
*/
private void ensureCapacity(int newCapacity) {
if (textBuffer.length<=newCapacity) {
Character[] newBuffer = new Character[textBuffer.length*2];
for (int i=0; i<size; i++) newBuffer[i] = textBuffer[i];
textBuffer = newBuffer;
}
}
/** readFromFile opens the text file with the given name and reads all of it into
textBuffer, including newlines.
@param fname is the name of the text file in the current directory.
*/
public void readFromFile(String fname) {
try{BufferedReader iStream
= new BufferedReader(
new FileReader(fname)
);
String stringIn;
Scanner sc = new Scanner(iStream);
while (sc.hasNext())
{stringIn=sc.nextLine();
ensureCapacity(size+stringIn.length()+1);
for (int i=0; i<stringIn.length(); i++)
textBuffer[size++] = stringIn.charAt(i);
textBuffer[size++] = ' ';
}
iStream.close();
}
catch (FileNotFoundException e)
{System.out.println("Did not find "+fname);}
catch (IOException e) {}
}
/** iterator returns an iterator for the input file. */
public Iterator<Character> iterator() {
return new FileIterator();
}

/** The private inner class FileIterator implements an Iterator of Characters */
private class FileIterator implements Iterator<Character> {
/** curr is the current position in the textBuffer array */
private int curr;
public FileIterator() {
}
/** next() returns the next Character and updates the current index
@return the next Character from the file
*/
public Character next() {
return textBuffer[curr++];
}
/** hasNext() returns whether there is another Character
@return true if there is another Character in the buffer, else false.
*/
public boolean hasNext() {
return curr<size;
}
/** remove() is not implemented
@throws UnsupportedOperationException
*/
public void remove() {
throw new UnsupportedOperationException("remove is not supported");
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a filename: ");
String fname=sc.nextLine();
TextFileReader f = new TextFileReader(fname);
for (Character ch:f) System.out.print(ch);
}
}

The code must be completed how i asked to get full points. The code must include a comment for each method describing the method, a comment for each parameter describing what the parameter represents, and a comment for each variable declaration (including instance variables) describing what the variable represents.

Explanation / Answer

package File; import java.io.*; import java.util.*; /** * TextFileReader is an Iterable class that can be used to iterate through a * text file, one Character at a time. * * @author Bruce Martin */ public class TextFileReader implements Iterable { /** textBuffer is an array of Character elements from the text file. */ private Character[] textBuffer; /** size is the number of Characters in textBuffer */ int size; /** * A one-String-parameter constructor opens the file represented by the * String, from the same directory as the program file, and reads the * contents into textBuffer. * * @param fname * is the name of the text file. */ public TextFileReader(String fname) { textBuffer = new Character[100]; readFromFile(fname); } /** * ensureCapacity doubles the capacity of the Character array and copies the * old content to it, replacing the textBuffer array, if the current array * is less than the new capacity needed. * * @param newCapacity * is the capacity requested */ private void ensureCapacity(int newCapacity) { if (textBuffer.length
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote