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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started