Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Awt implements ActionListener
{
	JFrame f;
	JButton b1,b2,b3,b4,b5;
	JTextField t1,t2,t3;
	JLabel l,l1;
	Awt()
	{
		f=new JFrame("Listener");
		t1=new JTextField("             ");
		t2=new JTextField("             ");
		t3=new JTextField("             ");
		b1=new JButton("Add");
		b2=new JButton("Sub");
		b3=new JButton("Mul");
		b4=new JButton("Div");
		l=new JLabel();
		l1=new JLabel();
	}
	public void awt1()
	{
		f.setLayout(new GridLayout(3,2));
		f.setVisible(true);
		f.add(t1);
		f.add(t2);
		f.add(t3);
		f.add(b1);
 		f.add(b2);
		f.add(b3);
		f.add(b4);
		f.add(l);
		f.add(l1);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		f.pack();
	}
	public void actionPerformed(ActionEvent e)
	{
		String s=new String(e.getActionCommand());
		l.setText(s);
		if((s).equals("Add"))
		{
			int  a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a+b;
                        t3.setText(c.toString());
 
		}
		else if((s).equals("Sub"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a-b;
			t3.setText(c.toString());
	
		}
		else if((s).equals("Mul"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a*b;
			t3.setText(c.toString());
		}
	}
	
	public static void main(String args[])
	{
		Awt a=new Awt();
		a.awt1();
	}
}
 
Hi..
This code runs successfully but when I entered input and press a button a number format exception occurred. I don't know where I made a mistake, please spot that logical error in this code.
Please help me.
Posted 19-Nov-10 19:08pm
ammu23372
Edited 22-Nov-10 0:25am
Nagy Vilmos259.9K
v4
Comments
Abhinav S at 20-Nov-10 1:45am
   
Corrected pre tags.
Richard MacCutchan at 20-Nov-10 12:28pm
   
I have no idea why this question was 1-voted, but I have given it a 5 to compensate. There really are some silly people about.
Nagy Vilmos at 22-Nov-10 6:25am
   
Fixed typing - all caps is considered rude.
Member 9355851 at 13-Mar-13 13:24pm
   
Bro,you havent called the actionPerformed() function!!!how will it perform those operations without the logic!!#noob
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The strings returned by getText() contain trailing spaces which parseInt() objects to. Change your code to something like:
String stemp = t1.getText().trim();
int  a=Integer.parseInt(stemp);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The strings returned by getText() contain trailing spaces which parseInt() objects to. Change your code to something like:
 
String stemp = t1.getText().trim();
int a=Integer.parseInt(stemp);
 

Also put the validations for all the getText() methods, meaning proceed further only if the value returning from getText() is not null or not empty.
  Permalink  
Comments
TorstenH. at 16-Apr-13 3:09am
   
Question is from Nov. 2010.
Richard MacCutchan at 16-Apr-13 6:28am
   
Why have you posted this? It is an exact copy of my answer of more than two years ago!
saigowthami at 16-Apr-13 6:31am
   
i dint post i just answered thats it
aspnet_regiis -I at 4-May-13 14:51pm
   
Marked as repost
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As a minor advice. Why have you initialized your text fields with so many space characters? If you want the text fields to be empty, the just initialize them like this:
 
 
     JTextField myTextField=new JTextField("");
 
 
Thus the compiler knows that text fields must be empty. And by doing so, you won't be needing trim() method when parsing the value from the text fields.
 
And also in the actionPerformed() method with getting the name of the event, rather i would just use:
 
   if(e.getSource()==bt1){
 
     //do something -> add, mul etc.
}
 
Other than that, the code seems fine.
 
Best regards.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 265
2 Nguyen.H.H.Dang 230
3 Peter Leow 195
4 arvind mepani 187


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 13 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid