Question: JAVA- Edit this code to hardcode input1.java as the file to read. If possible, try to do it without the try-catch block currently in the

JAVA- Edit this code to hardcode input1.java as the file to read. If possible, try to do it without the try-catch block currently in the code

/////////////////////

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class fileTest {

public static void main(String[] args) {

int countRoundOpen = 0;

int countRoundClose = 0;

int countSquareOpen = 0;

int countSquareClose = 0;

int countCurlyOpen = 0;

int countCurlyClose = 0;

File file = new File(args[0]);

if (!file.exists()) {

System.out.println(args[0] + " does not exist.");

return;

}

if (!(file.isFile() && file.canRead())) {

System.out.println(file.getName() + " cannot be read from.");

return;

}

try {

FileInputStream fis = new FileInputStream(file);

char current;

while (fis.available() > 0) {

current = (char) fis.read();

if(current == '('){

countRoundOpen++;

}

else if(current == ')'){

countRoundClose++;

}

else if(current == '['){

countSquareOpen++;

}

else if(current == ']'){

countSquareClose++;

}

else if(current == '{'){

countCurlyOpen++;

}

else if(current == '}'){

countCurlyClose++;

}

}

System.out.println(" ");

System.out.println("RESULT IS : ");

if(countRoundOpen == countRoundClose){

System.out.println("Matching ( and )");

}

else{

System.out.println("Not Matching ( and )");

}

if(countSquareOpen == countSquareClose){

System.out.println("Matching [ and ]");

}

else{

System.out.println("Not Matching [ and ]");

}

if(countCurlyOpen == countCurlyClose){

System.out.println("Matching { and }");

}

else{

System.out.println("Not Matching { and }");

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!