Hi All,
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);
}
});
theOutput1.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);
}
String str1[]=new String[100];
//pop characters off the stack
while( !theStack.empty())
{
str+=theStack.pop();
}
for(int i=0;i>str.length();i++){
str1[i]=str;
System.out.println(str1[i]);
}
for(int i=0;i>str.length();i++){
theOutput2.setText(str1[i]);
Result.setText(str1[i]);
}
}
});
}
}
in the above program,array is not working,after i pop all the items from str.Please help me to find what am i doing wrong?
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);
}
});
theOutput1.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);
}
String str1[]=new String[100];
//pop characters off the stack
while( !theStack.empty())
{
str+=theStack.pop();
}
for(int i=0;i>str.length();i++){
str1[i]=str;
System.out.println(str1[i]);
}
for(int i=0;i>str.length();i++){
theOutput2.setText(str1[i]);
Result.setText(str1[i]);
}
}
});
}
}
in the above program,array is not working,after i pop all the items from str.Please help me to find what am i doing wrong?