Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with reading from .dat file

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    // 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.
    //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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Need help with reading from .dat file

    Recommended reading: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Json (May 5th, 2011)

Similar Threads

  1. Reading from a file
    By NeedzABetterSN in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2011, 07:07 AM
  2. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  3. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 03:43 AM
  4. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM