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

Where do I put the input.txt file to be read for this java code in NetBeans? Ple

ID: 3917074 • Letter: W

Question

Where do I put the input.txt file to be read for this java code in NetBeans? Please give detailed instructions.


package postfixcalculator;

import java.io.*;

public class PostfixCalculator {

int result, opr1, opr2, opr3, i, num = 0;

public void evaluatePostfix(String inputFile, String outputFile) {

try {
Stack st = new Stack();
BufferedReader br = new BufferedReader(new FileReader(inputFile));
BufferedWriter fout = new BufferedWriter(new FileWriter(outputFile));
String postfix = br.readLine();
while (postfix != null) {
i = 0;
while (i < postfix.length()) {
// if character scanned is space skip that character
if (Character.isSpace(postfix.charAt(i)) || postfix.charAt(i) == ' ') {
i++;
continue;
}
// if character scanned is digit or alphabet loop through digit or alphabet and add it to target string
if (Character.isDigit(postfix.charAt(i))) {
num = 0;
while (Character.isDigit(postfix.charAt(i))) {
num = (num * 10) + postfix.charAt(i) - '0';
i++;
}
st.push(num);
} else {
opr1 = st.pop();
opr2 = st.pop();
switch (postfix.charAt(i)) {
case '+':
opr3 = opr2 + opr1;
break;
case '-':
opr3 = opr2 - opr1;
break;
case '*':
opr3 = opr2 * opr1;
break;
case '/':
opr3 = opr2 / opr1;
break;
case '%':
opr3 = opr2 % opr1;
break;
case '$':
opr3 = (int) Math.pow(opr2, opr1);
break;
}
i++;
st.push(opr3);
}
}
// pop back the remaining operators and add it to target string
while (st.top != -1) {
int opr = st.pop();
result = opr;
}
fout.write(result + " "); //write the result to the output file
postfix = br.readLine();
}
fout.close();
} catch (Exception e) {
}
System.out.println("Results written to the file");

}

public static void main(String[] args) {
PostfixCalculator obj = new PostfixCalculator();
obj.evaluatePostfix("input.txt", "output.txt");
}
}

package postfixcalculator;

class Stack {

int top, max_opr = 50; //declare top
int[] stackarr = new int[max_opr]; //allocate memory for stack

public Stack() {
top = -1;
}

public void push(int item) {
if (top == max_opr - 1) {
System.out.println("stack is full");
} else {
top = top + 1;
stackarr[top] = item;
}
}

public int pop() {
int item;
if (top == -1) {
return -1;
} else {
item = stackarr[top];
top = top - 1;
return item;
}
}
}

Explanation / Answer

obj.evaluatePostfix("input.txt", "output.txt");
The way you are passing the file is corrrect. This works when you place where you code and inut.txt exists in the same folder.
For example if you code folder id c://src then if you give like input.txt it will work. But it is always recommended to give full path of file
where yoy place the file like code c://src/input.txt.
Note: It is always recommended to have inout and output files to be in the same code folder and to give the full path of file.

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