It is becoming frustrating. Somebody help me!!!!

Action Executed:

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 what i tried to type out were just the first few lines. I need help.

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 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();

}

publicvoid 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));

}

}

publicstaticvoid main (String arg [ ])

{

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

ObjFr.setDefaultCloseOperation(EXIT_ON_CLOSE);

ObjFr.setSize (400, 400);

ObjFr.setVisible (true);

}

}

[5862 byte] By [bakesa] at [2007-11-27 4:03:35]
# 1
stop shouting and crying...camickr gave you an answer yesterday...and for your exception, everything is said:java.lang.NumberFormatException: For input string: "5n!" incremented code: line 107
suparenoa at 2007-7-12 9:08:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

> stop shouting and crying...

> camickr gave you an answer yesterday...

>

> and for your exception, everything is said:

> java.lang.NumberFormatException: For input string:

> "5n!"

> incremented code: line 107

This was my earlier attempt:

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

Camickr's Response:

> 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 9:08:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

try this..

public void actionPerformed(ActionEvent ae)

{

if (ae.getSource()==jbtnFT)

{

String num1=jTfd.getText();

int num= Integer.parseInt(num1);

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.setText(jbtn.getActionCommand());

}

}

Athia at 2007-7-12 9:08:21 > top of Java-index,Desktop,Core GUI APIs...
# 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 9:08:21 > top of Java-index,Desktop,Core GUI APIs...