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

You are always running into trouble with mismatched braces in your Java programs

ID: 646341 • Letter: Y

Question

You are always running into trouble with mismatched braces in your Java programs, and decide to write a program that will help you to find your errors (if only you can match the braces so THIS one will work!). The program will read any number of lines of text. It will output the same text, but with numbers inserted after each opening and closing brace that will help you to match them up. Each opening brace will be assigned a number consecutively based on the current number of unmatched opening braces. Each closing brace will be given the same number as the matching opening brace. If a closing brace is encountered when there are no unmatched opening braces, it will be labeled with a 0. The input to this program is the file to be processed and your program MUST first attempt to read that file from command line arguments. Be sure that your program is designed robustly and does not crash if the user forgets to enter the file name when running the program or if the file is not present, or other silly user error occurs. The output will be the lines of text with the braces numbered.   Make sure your program is well documented - include a printHeading() method that is called from the main method; and follow all previous instructions about method and file names, identifiers, etc.

looking at the other answer to this it is unable to run, and has no ways to handle errors, any help with this is appreciated

Explanation / Answer

Java Program:

import java.io.*;
import java.util.Scanner;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
System.out.println("Enter file name (must include full path name) : "); //must include full directory name to work correctly, for example C:UsersCDesktop¦Project 6Playlist.txt
Scanner scner = new Scanner(System.in); //scan in
String filename = scner.nextLine(); //file contents
FileInputStream fstream = new FileInputStream(filename);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in), 1);
String strLine;
//Read File Line By Line
//while ((strLine = br.readLine()) != null)
//{
// Print the content on the console
int j=0;
int k=0;
String string1 = "{", string2 = "}";
boolean bool1, bool2;
for (int i=0;i<20;i++)
{
strLine = br.readLine();
bool1 = isSubstring(strLine, string1);
bool2 = isSubstring(strLine, string2);
if (bool1 == true)
{
System.out.println ((strLine) + " " + (j)) ;
j++;
k=j;
}
else if (bool2 == true)
{
if(k <= 0)
{
k=0;
System.out.println ((strLine) + " " + (k)) ;
}
else
{
System.out.println ((strLine) + " " + (k-1)) ;
k--;
}
}
else
{
System.out.println (strLine) ;

}
}
//System.out.println (strLine);
//}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static boolean isSubstring( String str1, String str2 )
{
if (str1.indexOf(str2) >= 0) //using indexOf, see if string2 is contained in string1. if not -1 will be returned by indexOf, resulting in a return value of false
return true;  
else
return false;
}
}

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