Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I have a problem in array,please help me  RSS feed

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i went trough yr code and found that your for loop condition is wrong...

code:

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];
//String str1[];

//pop characters off the stack
while( !theStack.empty())
{

str+=theStack.pop();

}
//Your condition is not true
for(int i=0;i>str.length();i++){
str1[i]=str;
}
for(int i=0;i>str.length();i++){
theOutput2.setText(str1[i]);
Result.setText(str1[i]);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ This is the correct condition @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
for(int i=0;i<str.length();i++){
str1[i]=str;
}
for(int i=0;i<str.length();i++){
theOutput2.setText(str1[i]);
Result.setText(str1[i]);
}

}
});
}
}

regards
KKH
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!