Forums Register Login

I have a problem in array,please help me

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 704 times.
Similar Threads
Stacks Need Help
Can't Update Record ?
Can't Update Record ?
I have a problem in array,please help me
I'm having trouble with running Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:50:06.