The Artima Developer Community
Sponsored Link

Java Answers Forum
How to use Array to get every character from Stack.pop()

2 replies on 1 page. Most recent reply: Feb 6, 2006 10:59 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Madhan Kumar

Posts: 10
Nickname: madhan
Registered: Mar, 2002

How to use Array to get every character from Stack.pop() Posted: Feb 6, 2006 8:25 AM
Reply to this message Reply
Advertisement
Hi,

I am using Stack class to calculate simple arithmetic expressions involving integers,
such as 1+2*3.your program would execute operations in the order given,without regarding to the precedence of operators.
*Thus, the expression 1+2*3 should be calculated (1+2)*3=9,not 1+(2*3)=7.

import java.awt.*;
import java.awt.event.*;
 
import javax.swing.*;
import java.util.Stack;
 
public class Ex15_2 extends JApplet{
    Stack theStack=new Stack();
    JTextField theInput=new JTextField();
    JTextField theOutput1=new JTextField();
    JTextField theOutput2=new JTextField();
    JTextField Result=new JTextField();
    public void init(){
        Container c=getContentPane();
        c.setLayout(new GridLayout(7,1));
        c.add(new JLabel("Input"));
        c.add(theInput);
        c.add(new JLabel("Stack1 Output"));
        c.add(theOutput1);
         c.add(new JLabel("Stack2 Output"));
        c.add(Result);
        theInput.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt)
            {
                String str=theInput.getText();
                //push characters on to the stack
                while(str.length()>0){
                    theStack.push(str.substring(0, 1));
                    str=str.substring(1);
                }
                str="";
                //pop characters off the stack
                while(!theStack.empty())
                {
                    str+=theStack.pop();
                }
                theOutput1.setText(str);
            }
        });
        
        
        
        
        
        theInput.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt)
            {
                String str=theOutput1.getText();
                //push characters on to the stack
                while(str.length()>0){
                    theStack.push(str.substring(0, 1));
                    str=str.substring(1);
                }
                str="";
                int i=0;
                //pop characters off the stack
                while( !theStack.empty())
                {
                   
                    str+=theStack.pop();
                    
                }
               
            }
        });
    }
}


I couldn't figure out,theStack.pop() item ,how to get every item into the array,because one is integer,next is operator and so on.Can anyone have any idea,please?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to use Array to get every character from Stack.pop() Posted: Feb 6, 2006 10:54 PM
Reply to this message Reply
theStack.pop() only returns Strings with length 1. There are no integers yet. You still have to interpret them.

Where the line
   str+=theStack.pop();

is, you have to check the character.
You have allways to remember 2 numbers: the current result and the number in the cache. Also you have to remember wich operation was before the number in the cache.
If the charakter is a int, multiply the cache with 10 and add the int value. If you find a fraction separator (hope, this is the right expression, I'm not English), you must stop multiplying the cache, but divide the new value with 10 or 100 or 1000 (depends on it's position) and add it to the cache.
If you find an operator or there are no more characters, execute the previous operation, then store the new operation and set cache to 0.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to use Array to get every character from Stack.pop() Posted: Feb 6, 2006 10:59 PM
Reply to this message Reply
Hint: To store the operation, it's recommended to use a typesafe enum class. This makes the code easier to read.
private enum Operations {NOTHING, ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION};


At the beginning, you can set the previousOperation variable to null or NOTHING.
A alternative would be to remove NOTHING from the list and to set the first operation to ADDITION since no operator bvefore the first value means that the value i added to 0.

Flat View: This topic has 2 replies on 1 page
Topic: Problem in understanding three dimensional array Previous Topic   Next Topic Topic: Help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use