Need help with reading from .dat file
Hi I'm currently working on a programming assignment for my class. I was assigned to write a postfix calculator using stacks. I wrote my class, and it works with all examples I've thrown into my method. But the problem with my file is that my professor wants us to read the postfix expressions from a file called "in.dat" and I have no idea how to do that. We haven't learned this yet in my class, nor is it even in my textbook. I've looked up examples and it looks like I need the following imported in my class file, but I have no idea how to implement it into my code.
Code :
// import java.io.BufferedReader;
// import java.io.FileReader;
// import java.io.IOException;
// import java.util.StringTokenizer;
Here's the code if someone could help me with this, or provide me some links to examples that I would be able to learn from.
Code :
//Programming Assignment 4
import java.util.*;
import java.io.*;
import java.lang.Math;
/**
* All expressions are stored in a file called "in.dat". The file
* contains a sequence of arithmetic expressions in the postfix form,
* one per line. For example, the following file has two expressions:
*
* 2 3 ^ 35 5 / -
* 1 2 + 3 * # 4 - 5 6 - + _
*
* There is at least one space to separate the operators and operands.
* All operands and values are considered as doubles.
*
* The following are the operators:
*
* +, -, *, / arithmetic operators
* _ unary negation
* # square root
* ^ exponentiation (a b ^ = a raised to the power b)
*
* There is a carriage-return at the end of the last expression.
*/
public class PostFixExpression
{
/**
* Main method for class to test expression evaluation
*/
public static void main(String[] args)
{
System.out.println("Solution to... 2 3 ^ 35 5 / -"
+ evaluate("2 3 ^ 35 5 / -"));
System.out.println("Solution to... 1 2 + 3 * # 4 - 5 6 - + _:"
+ evaluate("1 2 + 3 * # 4 - 5 6 - + _"));
}
/**
* Evaluates the PostFix String
*
* @param expression</CODE>
* The expression being evaluated
* @return
* Last pop in the stack
*/
public static double evaluate(String expression)
{
Stack<Double> postFixStack = new Stack<Double>();
Scanner input = new Scanner(expression);
String part;
while(input.hasNext())
{
if(input.hasNextDouble())
{
postFixStack.push(input.nextDouble());
}
else
{
part = input.next();
double a, b;
for(int i = 0; i < part.length(); i++)
{
switch(part.charAt(i))
{
//Case for Addition
case '+':
a = postFixStack.pop();
b = postFixStack.pop();
postFixStack.push(b + a);
break;
//Case for Subtraction
case '-':
a = postFixStack.pop();
b = postFixStack.pop();
postFixStack.push(b - a);
break;
//Case for Multiplication
case '*':
a = postFixStack.pop();
b = postFixStack.pop();
postFixStack.push(b * a);
break;
//Case for Division
case '/':
a = postFixStack.pop();
b = postFixStack.pop();
postFixStack.push(b / a);
break;
//Case for Unary Negation
case '_':
a = postFixStack.pop();
postFixStack.push(a * -1);
break;
//Case for SquareRoot
case '#':
a = postFixStack.pop();
postFixStack.push(Math.sqrt(a));
break;
//Case for Power/Exponentiation
case '^':
a = postFixStack.pop();
b = postFixStack.pop();
postFixStack.push(Math.pow(b,a));
break;
}
}
}
}
return postFixStack.pop();
}
}
Thanks in advance to anyone who could help me.
Re: Need help with reading from .dat file