I need Help with codes. It isn't working

I had once writing a code for finding the factorial of a number in elementary programming with C. I tried to incorporate it into my work. However, it is not working.

Below are the major hitches:

1. I have a problem with entering two numbers by simply pressing the buttons. Once i try to input another number the second number displaces the first on the textfield. Which suffices to say that entering a two or three digit number via the buttons is not working.

2. What I intend to archieve is such once I have enter the numbers using the buttons i could get the factorial of whatever number i entered by simply pressing the n! button. Once i attempt pressing the n! button it displays error in the command prompt (a long list of error comments).

How should I Proceed.

Below is my full code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass PanFrameextends JFrameimplements ActionListener

{

JTextField jTfd =new JTextField(20);

JButton jbtn7 =new JButton ("7");

JButton jbtn8 =new JButton ("8");

JButton jbtn9 =new JButton ("9");

JButton jbtn4 =new JButton ("4");

JButton jbtn5 =new JButton ("5");

JButton jbtn6 =new JButton ("6");

JButton jbtn1=new JButton ("1");

JButton jbtn2 =new JButton ("2");

JButton jbtn3 =new JButton ("3");

JButton jbtn0 =new JButton ("0");

JButton jbtnFT =new JButton ("n!");

public PanFrame (String title)

{

super (title);

Panel pan1 =new Panel();

getContentPane().setLayout(new FlowLayout ());

pan1.add(jTfd);

Panel pan2 =new Panel();

getContentPane().setLayout (new GridLayout (4,5,2,2));

pan2.add (jbtn7);

pan2.add (jbtn8);

pan2.add (jbtn9);

pan2.add (jbtn4);

pan2.add (jbtn5);

pan2.add (jbtn6);

pan2.add (jbtn1);

pan2.add (jbtn2);

pan2.add (jbtn3);

pan2.add (jbtn0);

pan2.add (jbtnFT);

getContentPane().add(pan1);

getContentPane().add(pan2);

//adding the Listener

jTfd.addActionListener(this);

jbtn7.addActionListener(this);

jbtn8.addActionListener(this);

jbtn9.addActionListener(this);

jbtn4.addActionListener(this);

jbtn5.addActionListener(this);

jbtn6.addActionListener(this);

jbtn1.addActionListener(this);

jbtn2.addActionListener(this);

jbtn3.addActionListener(this);

jbtn0.addActionListener(this);

jbtnFT.addActionListener(this);

validate();

this.addWindowListener (new WindowAdapter()

{

publicvoid windowClosing (WindowEvent we)

{

setVisible (false);

System.exit (0);

}

});

}

publicvoid actionPerformed(ActionEvent ae)

{

jTfd.setText(ae.getActionCommand());

if(ae.getActionCommand()=="n!")

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

publicstaticvoid main (String arg [ ])

{

PanFrame ObjFr =new PanFrame ("Two Panel in One Frame");

ObjFr.setSize (400, 400);

ObjFr.setVisible (true);

}

}

bakes

[5845 byte] By [bakesa] at [2007-11-27 3:29:31]
# 1

> the second number displaces the first on the textfield.

Yes, that is the purpose of the setText() method. You want to append text to the document. This posting shows a trick you can use:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=609795

> Once i attempt pressing the n! button it displays error...

The functionality of the N! button is different than the functionality for clicking on the other buttons. So...

a) you need two different ActionListeners so your code doesn't get confusing

b) you need an "if" statement "AT THE BEGINING" of the actionPerformed to identify which button was clicked.

The error message tells you that the problem is on line 78. Did you print the value of the variables you are using on the statement to see if they contain the correct data?

camickra at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 2

> a) you need two different ActionListeners so your

> code doesn't get confusing

I thought ActionListener public void actionPerformed(ActionEvent) is what i should use for buttons. Do you imply i should create two with different object for example (ActionEvent e) and (ActionEvent ae)>

> b) you need an "if" statement "AT THE BEGINING" of

> the actionPerformed to identify which button was

> clicked.

> do you mean that i have to insert an if statement in both ActionListeners? I do not understand please help me out i am new to java. This thing is getting me more and more confused

bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 3
Sorry, I meant you need to do A or B to solve your problem.> jTfd.setText(ae.getActionCommand());The above line of code should not be executed when you click the factorial button.
camickra at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 4
How should i implement the ActionListeners that will perform the separate function:1. Register a number on the textfield2 execute the factorial formula once the n! button is pressed?Please I need help. I gett confused.
bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 5

This is what i attempted doing:

public void actionPerformed(ActionEvent ae)

{if (jTfd.replaceSelection( jbtn.getActionCommand() )=="n!")

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

else

JButton jbtn = (JButton)ae.getSource();

jTfd.replaceSelection( jbtn.getActionCommand() );

}

it gave me the following error messages:

java:116 not a statement JButton jbtn=(JButton)ae.getsource();

java:116 ';' expected JButton jbtn = (JButton)ae.getSource();

am confused

bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 6

> if (jTfd.replaceSelection( jbtn.getActionCommand() )=="n!")

What is this? Why are you using the replaceSelection() method here? Did you read the API to understand what the method does?

Next fix your code so that the formatting displays correctly. Sometimes you use tabs and sometimes you use spaces to indent each new line. So the formatting on the forum shows up strange.

Once you do this you might notice that you a missing an "{" for your else statement.

Finally, you don't use "==" to compare two objects. You use the equals(...) method.

camickra at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 7

With the code below i can queue in the numbers i want using he keyboard and once i punch the n! it gives me the factorial. However what i want is to be able to key in the number using the buttons available as well as being to use the n! to produce the factorial. how should i proceed?

public void actionPerformed(ActionEvent ae)

{

if (ae.getSource()==jbtnFT)

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 8

Perhaps something like this?

private String tempText = "";

public void actionPerformed(ActionEvent ae) {

if (jTfd.replaceSelection( jbtn.getActionCommand() )=="n!") {

tempText = "";

long fact=1;

for(int i=1; i<=Integer.parseInt(jTfd.getText()); i++)

fact *=i;

jTfd.setText(String.valueOf(fact));

}

else {

tempText += ((JButton)ae.getSource()).getActionCommand();

jTfd.replaceSelection(tempText);

/*

* // Alternately, you can try:

* jTfd.setText(jTfd.getText() + ((JButton)ae.getSource()).getActionCommand());

*

*/

}

}

FBLa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 9

> Perhaps something like this?

>

> > private String tempText = "";

> public void actionPerformed(ActionEvent ae) {

> if (jTfd.replaceSelection( jbtn.getActionCommand()

> ) )=="n!") {

> tempText = "";

> long fact=1;

> for(int i=1; i<=Integer.parseInt(jTfd.getText());

> ); i++)

> fact *=i;

> jTfd.setText(String.valueOf(fact));

> }

> else {

> tempText +=

> += ((JButton)ae.getSource()).getActionCommand();

> jTfd.replaceSelection(tempText);

> /*

> * // Alternately, you can try:

> * jTfd.setText(jTfd.getText() +

> ) + ((JButton)ae.getSource()).getActionCommand());

> *

> */

> }

> }

I tried out what you suggested above but it gave the following eror:

PanFrame.java103:cannot resolve symbol

symbol: variable jbtn

location: class PanFrame

if (jTfd.replaceSelection( jbtn.getActionCommand() )=="n!")

'void' type not allowed here

if (jTfd.replaceSelection( jbtn.getActionCommand() )=="n!")

I do not think "if(jTfd.replaceSelection(jbtn.getActionCommand())=="n!")"

is the correct way of expressing what i intend to archieve. When i first wrote it out it did not make sense to me but i could not find an alternative. so i said let me give it a triy.

i have also attempted to structure it this way

public void actionPerformed(ActionEvent ae)

{

JButton jbtn = (JButton)ae.getSource();

jTfd.replaceSelection( jbtn.getActionCommand() );

if (ae.getSource()==jbtnFT)

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

the action i performed having structured it as stated above was: I pressed the button 5 and it displayed 5 in the textfield then i wanted to find the factorial of 5. so i pressed the (n!) button. instead of calculating the factorial of 5 and displaying the output in the textfield, the factorial symbol was displayed after the number 5 and error messages were displayed in the command prompt. i have tried to type out some of the messages below:

java.lang.NumberFormatException: For input string: "5n!" at java.lang.NumberFormat Exception.forInputString(NumberFormatException.java.48)

at java.lang.Integer.parseInt(Integer.java: 477)

at java.lang.Integer.parseInt(Integer.jaa:518)

at PanFrame.actionPerformed(Drum.java:107)

at javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

Please could you do me favor i am posting the entire code below: copy it and run it then you can see the error messages in its entirety. It is quite lenghty was i tried to type out were jus the first few lines.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class PanFrame extends JFrame implements ActionListener

{

JTextField jTfd =new JTextField(20);

JButton jbtn7 = new JButton ("7");

JButton jbtn8 = new JButton ("8");

JButton jbtn9 = new JButton ("9");

JButton jbtnFT = new JButton ("n!");

JButton jbtn4 = new JButton ("4");

JButton jbtn5 = new JButton ("5");

JButton jbtn6 = new JButton ("6");

JButton jbtn1= new JButton ("1");

JButton jbtn2 = new JButton ("2");

JButton jbtn3 = new JButton ("3");

JButton jbtn0 = new JButton ("0");

public PanFrame (String title)

{

super (title);

Panel pan1 = new Panel();

getContentPane().setLayout(new FlowLayout ());

pan1.add(jTfd);

Panel pan2 = new Panel();

getContentPane().setLayout (new GridLayout (4,5,2,2));

pan2.add (jbtn7);

pan2.add (jbtn8);

pan2.add (jbtn9);

pan2.add (jbtnFT);

pan2.add (jbtn4);

pan2.add (jbtn5);

pan2.add (jbtn6);

pan2.add (jbtn1);

pan2.add (jbtn2);

pan2.add (jbtn0);

getContentPane().add(pan1);

getContentPane().add(pan2);

//adding the Listener

jTfd.addActionListener(this);

jbtn7.addActionListener(this);

jbtn8.addActionListener(this);

jbtn9.addActionListener(this);

jbtnFT.addActionListener(this);

jbtn4.addActionListener(this);

jbtn5.addActionListener(this);

jbtn6.addActionListener(this);

jbtn1.addActionListener(this);

jbtn2.addActionListener(this);

jbtn3.addActionListener(this);

jbtn0.addActionListener(this);

validate();

}

public void actionPerformed(ActionEvent ae)

{

JButton jbtn = (JButton)ae.getSource();

jTfd.replaceSelection( jbtn.getActionCommand() );

if (ae.getSource()==jbtnFT)

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

public static void main (String arg [ ])

{

PanFrame ObjFr = new PanFrame ("Two Panel in One Frame");

ObjFr.setDefaultCloseOperation(EXIT_ON_CLOSE);

ObjFr.setSize (400, 400);

ObjFr.setVisible (true);

}

}

bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 10

> > if (jTfd.replaceSelection( jbtn.getActionCommand()

> )=="n!")

>

> What is this? Why are you using the

> replaceSelection() method here? Did you read the API

> to understand what the method does?

>

> Next fix your code so that the formatting displays

> correctly. Sometimes you use tabs and sometimes you

> use spaces to indent each new line. So the formatting

> on the forum shows up strange.

>

> Once you do this you might notice that you a missing

> an "{" for your else statement.

>

> Finally, you don't use "==" to compare two objects.

> You use the equals(...) method.

I did realize that I left out an opening curly brace which i have corrected. Now here is my most recent attempt that i posted a while ago that gave me errors:

public void actionPerformed(ActionEvent ae)

{

JButton jbtn = (JButton)ae.getSource();

jTfd.replaceSelection( jbtn.getActionCommand() );

if (ae.getSource()==jbtnFT)

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

If there is something that is eluding my grasp please point it out to me. It is not that I enjoying nagging. It is just that the more I try the more it eludes me that is what is getting to me.

bakesa at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 11

I am not sure if your problem is solved.But this worked just perfect for me.I jumped in late and I haven't looked into all the posting.

public void actionPerformed(ActionEvent ae)

{

if ( !ae.getActionCommand().equals("n!") )

jTfd.setText(ae.getActionCommand());

if(ae.getActionCommand()=="n!")

{

int num= Integer.parseInt(jTfd.getText());

int i;

long fact=1;

for(i=1; i<=num; i++)

{

fact *=i;

}

jTfd.setText(String.valueOf(fact));

}

}

cantrya at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...
# 12

see http://forum.java.sun.com/thread.jspa?threadID=5171415&messageID=9659826#9659826

Reply 4/4

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class PanFrame extends JFrame implements ActionListener {

JTextField jTfd = new JTextField(20);

JButton jbtn7 = new JButton("7");

JButton jbtn8 = new JButton("8");

JButton jbtn9 = new JButton("9");

JButton jbtnFT = new JButton("n!");

JButton jbtn4 = new JButton("4");

JButton jbtn5 = new JButton("5");

JButton jbtn6 = new JButton("6");

JButton jbtn1 = new JButton("1");

JButton jbtn2 = new JButton("2");

JButton jbtn3 = new JButton("3");

JButton jbtn0 = new JButton("0");

JButton jbtnReset = new JButton("Reset");

public PanFrame(String title) {

super(title);

Panel pan1 = new Panel();

getContentPane().setLayout(new FlowLayout());

jTfd.setEditable(false);///////////////AVOID INCORRECT INPUT

pan1.add(jTfd);

Panel pan2 = new Panel();

getContentPane().setLayout(new GridLayout(4, 5, 2, 2));

pan2.add(jbtn7);

pan2.add(jbtn8);

pan2.add(jbtn9);

pan2.add(jbtnFT);

pan2.add(jbtn4);

pan2.add(jbtn5);

pan2.add(jbtn6);

pan2.add(jbtn0);

pan2.add(jbtn1);

pan2.add(jbtn2);

pan2.add(jbtn3);

pan2.add(jbtnReset);

getContentPane().add(pan1);

getContentPane().add(pan2);

// adding the Listener

jTfd.addActionListener(this);

jbtn7.addActionListener(this);

jbtn8.addActionListener(this);

jbtn9.addActionListener(this);

jbtnFT.addActionListener(this);

jbtn4.addActionListener(this);

jbtn5.addActionListener(this);

jbtn6.addActionListener(this);

jbtn1.addActionListener(this);

jbtn2.addActionListener(this);

jbtn3.addActionListener(this);

jbtn0.addActionListener(this);

jbtnReset.addActionListener(this);

validate();

}

public void actionPerformed(ActionEvent ae) {

JButton jbtn = (JButton) ae.getSource();

if (ae.getSource() == jbtnFT) {

try {

int num = Integer.parseInt(jTfd.getText());

if(num<25){///////AVOID OVERFLOW PROBLEMS

int i;

long fact = 1;

for (i = 1; i <= num; i++) {

fact *= i;

}

jTfd.setText(""+fact);

}else{

jTfd.setText("");

}

} catch (Exception e) {

jTfd.setText("");

}

}else if(ae.getSource()==jbtnReset){

jTfd.setText("");

}else{

jTfd.replaceSelection(jbtn.getActionCommand());

}

}

public static void main(String arg[]) {

PanFrame ObjFr = new PanFrame("Two Panel in One Frame");

ObjFr.setDefaultCloseOperation(EXIT_ON_CLOSE);

ObjFr.setSize(400, 400);

ObjFr.setVisible(true);

}

}

java_2006a at 2007-7-12 8:32:31 > top of Java-index,Desktop,Core GUI APIs...