Results 1 to 9 of 9
Thread: Computing Values in TextField
- 02-09-2012, 09:34 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Computing Values in TextField
Hi,
I need help to compute the values on a TextField by converting it into Int,
this is the code and I got no other clues on doing this...
or post a link that has the answers tnx..
Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class advjav extends JFrame{ double add,a,b; JButton compute=new JButton("COMPUTE"); JTextField tf=new JTextField(); JTextField tf2=new JTextField(); JLabel jl=new JLabel("Input first No."); JLabel jl2=new JLabel("Input second No."); JPanel jp=new JPanel(new GridLayout(4,1)); public advjav()throws Exception{ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jp.add(jl); jp.add(tf); jp.add(jl2); jp.add(tf2); jp.add(compute); compute.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ String i=tf.getText(); int s=Integer.valueOf(i); tf.setText(i); String b=tf2.getText(); int c=Integer.valueOf(b); tf2.setText(b); System.out.print(""+(i+b)); }catch(Exception exp){} } }); getContentPane().add(jp, "Center"); pack(); } public static void main(String [] args)throws Exception{ new advjav().setVisible(true); } }Last edited by romanzo; 02-09-2012 at 10:11 AM.
- 02-09-2012, 09:48 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Computing Values in TextField
Please use code tags when posting code.Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class advjav extends JFrame{ double add,a,b; JButton compute=new JButton("COMPUTE"); JTextField tf=new JTextField(); JTextField tf2=new JTextField(); JLabel jl=new JLabel("Input first No."); JLabel jl2=new JLabel("Input second No."); JPanel jp=new JPanel(new GridLayout(4,1)); public advjav()throws Exception{ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jp.add(jl); jp.add(tf); jp.add(jl2); jp.add(tf2); jp.add(compute); compute.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ String i=tf.getText(); int s=Integer.valueOf(i); tf.setText(i); String b=tf2.getText(); int c=Integer.valueOf(b); tf2.setText(b); System.out.print(""+(i+b)); }catch(Exception exp){} } }); getContentPane().add(jp, "Center"); pack(); } public static void main(String [] args)throws Exception{ new advjav().setVisible(true); } }
Are you getting any errors?
If so what are they and what lines are causing them?
People aren't going to run this to see what the problem is.
- 02-09-2012, 10:04 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Computing Values in TextField
Sorry about the tags but there are no run time error i just want to know what codes to use to convert the string value on the TextField to Integer.
- 02-09-2012, 10:33 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Computing Values in TextField
Have a look at the API for Integer.
- 02-09-2012, 10:34 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Computing Values in TextField
Oh yes, one thing, don't do this:
Because if you get an exception you'll never know about it.Java Code:}catch(Exception exp){}
At the least do this:
Java Code:} catch(Exception exp) { exp.printStackTrace(); }
- 02-09-2012, 10:40 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Computing Values in TextField
thanks for the help tolls...
- 02-09-2012, 12:06 PM #7
Re: Computing Values in TextField
Nothing advanced about this question. Moving to New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-09-2012, 12:42 PM #8
Re: Computing Values in TextField
I dont know what i say is foolishness coz this is something basic. Have you tried this?
String s = tf.getText();
int a = Integer.parseInt(s);
- 02-09-2012, 01:00 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Computing bytes
By captainjack in forum New To JavaReplies: 5Last Post: 10-28-2011, 01:22 AM -
Allowing only hex values in a textfield
By elsanthosh in forum AWT / SwingReplies: 3Last Post: 07-20-2010, 09:31 AM -
hex values in textfield
By elsanthosh in forum AWT / SwingReplies: 2Last Post: 07-19-2010, 01:43 PM -
Computing the sum
By Limuh in forum New To JavaReplies: 3Last Post: 08-13-2008, 06:00 AM -
Allowing only numeric values in a TextField
By Java Tip in forum Java TipReplies: 0Last Post: 03-01-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks