Results 1 to 2 of 2
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Why is this stakc empty?

    So I am doing a reverse polish notation calculator, don't worry about how my algorithims work, I can fix them, but first I am lost as to why one of my stacks keeps coming up empty.

    Basic driver
    Java Code:
    import java.util.*;
    public class CalcStackDriver {
    
    
        public static void main(String[] args) {
         Stack j = new Stack();
         j.push("/");
         j.push(Integer.toString(3));
         j.push("+");
         j.push(Integer.toString(1));
         j.push(Integer.toString(2));
    
         CalcStack m = new CalcStack(j);
           m.solve();
    
        }
    
    }


    But the problem does not lie here, it lies in CalcStack

    Java Code:
    import java.util.*;
    
    public class CalcStack {
        Stack theStack;
        [B]Stack forprinting;[/B]
        double finalvalue=0;
        String temp10;
        String temp11;
        String temp12;
    
        public CalcStack(Stack j) {
        theStack=j;
      [B]  forprinting=j;[/B]
        }
    
        public double solve()  {
            
            try {
             while(!(theStack.empty())) {
            
            double d =0;
         
    
                String temp = ((String) theStack.pop());
                double j = Double.parseDouble(temp);
    
                String temp2 = ((String) theStack.pop()).toString();
                double m = Double.parseDouble(temp2);
    
                String temp3 = ((String) theStack.pop()).toString();
    
    
                if(temp3.equals("+")) {
                
                    d=j+m;
                    finalvalue=d;
                    String str = Double.toString(finalvalue);
                    theStack.push(str);
                    
                }
    
                if(temp3.equals("*")) {
                    d=j*m;
                    finalvalue=d;
                    theStack.push(d);
                }
    
                if(temp3.equals("/")) {
                    d=j/m;
                    finalvalue=d;
                    theStack.push(d);
                }
          
            }
        } catch (Exception e) {
           System.out.println(finalvalue);
        }
            printout();
    
              return finalvalue;
            }
    
    
    
    
        public void printout() {
    if(forprinting.empty()) {
        System.out.println("Your stack is empty");
        System.exit(0);
    }
    
            do{
              System.out.println("I got into the do loop");
              temp10 =((String) forprinting.pop());
              temp11= ((String) forprinting.pop());
              temp12= ((String) forprinting.pop());
    
             double j = Double.parseDouble(temp10);
             double v = Double.parseDouble (temp11);
             System.out.println("("+temp10+v+temp11+")");
    
    
    
    
            }   while(!(forprinting.empty())); {
            temp10=((String)forprinting.pop());
            temp12=((String)forprinting.pop());
    
            System.out.print(temp12+temp10);
    
    
            }
    
    
        }
    
        }


    Ok so here is the problem the forprinting stack is ALWAYS empty, but I don't understand how that is possible. The other stack, "theStack" has stuff in it, but the "forprinting" stack is empty? How is that possible? Anyone see anything I just cannot see?

    Here is the output
    Java Code:
    run:
    1.0
    Your stack is empty
    BUILD SUCCESSFUL (total time: 0 seconds)

  2. #2
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ok I took the try and catch out and I fixed it up some more I was adding a double to it instead of a string
    here is refined code
    Java Code:
    import java.util.*;
    
    public class CalcStack {
        Stack theStack;
        Stack forprinting;
        double finalvalue=0;
        String temp10;
        String temp11;
        String temp12;
    
        public CalcStack(Stack j) {
        theStack=j;
        forprinting=j;
        }
    
        public double solve()  {
            
            
             while(!(theStack.empty())) {
            
            double d =0;
         
    
                String temp = ((String) theStack.pop());
                double j = Double.parseDouble(temp);
    
                String temp2 = ((String) theStack.pop()).toString();
                double m = Double.parseDouble(temp2);
    
                String temp3 = ((String) theStack.pop()).toString();
    
    
                if(temp3.equals("+")) {
                
                    d=j+m;
                    finalvalue=d;
                    String str = Double.toString(finalvalue);
                    theStack.push(str);
                    
                }
    
                if(temp3.equals("*")) {
                    d=j*m;
                    finalvalue=d;
                    String str = Double.toString(finalvalue);
                    theStack.push(str);
                }
    
                if(temp3.equals("/")) {
                    d=j/m;
                    finalvalue=d;
                   String str = Double.toString(finalvalue);
                   theStack.push(str);
                }
          
            }
        
            
                printout();
              return finalvalue;
            }
    
    
    
    
        public void printout() {
    if(forprinting.empty()) {
        System.out.println("Your stack is empty");
        System.exit(0);
    }
    
            do{
              temp10 =((String) forprinting.pop());
              temp11= ((String) forprinting.pop());
              temp12= ((String) forprinting.pop());
    
             System.out.println("("+temp10+temp12+temp11+")");
    
    
    
    
            }   while(!(forprinting.empty())); {
            temp10=((String)forprinting.pop());
            temp12=((String)forprinting.pop());
    
            System.out.print(temp12+temp10);
            }
    
    
    
    
        }
    
        }
    I think I can get this...

Similar Threads

  1. NumberFormatException: empty String
    By svpriyan in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:46 AM
  2. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 08:08 PM
  3. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 08:28 PM
  4. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •