The user is getting a FileNotFoundException when running some Java code that is intended to read from an input.txt file and write to an output.txt file. Both files are saved in the same src folder as the Java file, but the code is still unable to locate the input file. The user asks for help to find a solution and recommends others try running the code themselves to see if they can identify where the problem lies.
I still keep getting this error message from the following code. I h.pdf
1. I still keep getting this error message from the following code. I have saved output.txt and
input.txt files in the same src folder where the Java file is and it still cannot locate the file. Please
help me find a solution. I recommend that you try running the code in your Java software with
the same text files saved in the directory to check and see if it runs and where the problem is.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class arithmeticCal {
private static class Stack {
private int[] data;
private int top;
private int maxSize;
public Stack(int s) {
maxSize=s;
data = new int[maxSize];
top = -1;
}
public void push(int x) {
data[++top]=x;
}
public int pop() {
if(top==-1){
return -1;
}
int x = data[top];
top--;
return x;
}
public int peek() {
2. return data[top];
}
public boolean isEmpty() {
return top == -1;
}
}
private static int precedence(char op) {
switch (op) {
case '(':
case ')':
return 0;
case '^':
return 1;
case '*':
case '/':
return 2;
case '+':
case '-':
return 3;
case '>':
case '<':
return 4;
case ',':
case '!':
return 5;
default:
return -1;
}
}
private static boolean isLeftAssociative(char op) {
return op != '^';
}
3. private static int applyOp(char op, int b, int a) {
switch (op) {
case '^':
return (int) Math.pow(a, b);
case '*':
return a * b;
case '/':
return a / b;
case '+':
return a + b;
case '-':
return a - b;
case '>':
return a > b ? 1 : 0;
case '<':
return a < b ? 1 : 0;
case ',':
return a + b;
case '!':
return a != b ? 1 : 0;
default:
throw new IllegalArgumentException("Invalid operator: " + op);
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
FileWriter writer = new FileWriter("output.txt");
String line;
while ((line = reader.readLine()) != null) {
String expression = line.trim();
Stack operandStack = new Stack(10);
Stack operatorStack = new Stack(10);
4. for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c >= '0' && c <= '9') {
int num = 0;
while (i < expression.length() && expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
num = num * 10 + (expression.charAt(i) - '0');
i++;
}
i--;
operandStack.push(num);
} else if (c == '(') {
operatorStack.push(c);
} else if (c == ')') {
while (operatorStack.peek() != '(') {
char op = (char) operatorStack.pop();
int b = operandStack.pop();
int a = operandStack.pop();
int result = applyOp(op, b, a);
operandStack.push(result);
}
operatorStack.pop(); // Pop the left parenthesis.
} else {
while (!operatorStack.isEmpty() && precedence(c) <= precedence((char) operatorStack.peek())
&& isLeftAssociative(c)) {
char op = (char) operatorStack.pop();
int b = operandStack.pop();
int a = operandStack.pop();
int result = applyOp(op, b, a);
operandStack.push(result);
}
operatorStack.push(c);
}
}
while (!operatorStack.isEmpty()) {
char op = (char) operatorStack.pop();
5. int b = operandStack.pop();
int a = operandStack.pop();
int result = applyOp(op, b, a);
operandStack.push(result);
}
int result = operandStack.pop();
writer.write(line + "n");
writer.write(result + "n");
}
reader.close();
writer.close();
} Exception in thread "main" java.io.FileNotFoundException Create breakpoint : input.txt (No
such file or directory) at java.base/java.io.FileInputStream.openo(Native Method) at
java.base/java.io.FileInputStream.open(FileInputStream.java:216) at
java.base/java.io.FileInputStream.(FileInputStream.java:157) at
java.base/java.io.FileInputStream.(FileInputStream.java:111) at
java.base/java.io.FileReader.(FileReader.java: 60) at arithmeticCal.main(arithmeticCal.java:95)