Results 1 to 8 of 8
Thread: newbie jtextfield question
- 12-23-2011, 01:08 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
newbie jtextfield question
so, i'm trying for a user to enter number in jtextfields and then when they press jbutton to add...the program takes the 2 numbers entered and adds them up....my program actually somewhat works but i'm converting from text to double and then back to text to be able to print the number in jlabel. I need your help in simplifying the code please. TKS.
public void actionPerformed(ActionEvent ae)
{
String text1 = one.getText();
double aDouble = Double.parseDouble(text1);
String text2 = two.getText();
double bDouble = Double.parseDouble(text2);
double answer = aDouble + bDouble;
String aString = Double.toString(answer);
testing.setText(aString);
}
});
- 12-23-2011, 01:34 AM #2
Re: newbie jtextfield question
What needs to be simplified?need your help in simplifying the code
It looks simple to me. Any changes would make it more complicated.
You have one statement for each step that needs to be taken.
- 12-23-2011, 05:00 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: newbie jtextfield question
Ok. cool. also, If the user enters something that's not a double/int I need to have it set to 0 in that case. I looked and can't find anything that will do that for me. How can i check if the user actually entered a double and if he/she didn't, use the value of 0?
- 12-23-2011, 05:32 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: newbie jtextfield question
parseDouble() will throw an exception if the string argument cannot be parsed as a double. Catch the exception, and set the value equal to zero in that case.
Double (Java Platform SE 6)
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
- 12-23-2011, 02:20 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: newbie jtextfield question
Ok. Tks. I found that online, thought there would be an easier way.....Let me try this and see what happens....TKS FOR THE HELP
- 12-23-2011, 06:53 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: newbie jtextfield question
Instead of catching exceptions, can i use a scanner let's say to identify if the input is a double? Wouldn't that be a more effective way of doing it?
- 12-23-2011, 07:07 PM #7
Re: newbie jtextfield question
Are you suggesting that You would take the String from the text field, create another object with the String and then ask that object if the data it has is a valid double?
What do you mean by "more effective"?
- 12-24-2011, 12:33 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: newbie jtextfield question
yeah, you're right.......I read the exceptions stuff and input in my program and works like a charm....thanks man...
Java Code:public void actionPerformed(ActionEvent ae) { String text1 = one.getText(); String text2 = two.getText(); try { aDouble = Double.parseDouble(text1); } catch (NumberFormatException e) { aDouble = 0; } try { bDouble = Double.parseDouble(text2); } catch (NumberFormatException e) { bDouble = 0; }
Similar Threads
-
Public JTextField question
By Someone001 in forum AWT / SwingReplies: 5Last Post: 10-26-2011, 02:45 PM -
JTextfield question
By xyknight in forum New To JavaReplies: 3Last Post: 04-08-2011, 04:25 PM -
JTextField Question
By Rocketz in forum New To JavaReplies: 1Last Post: 03-09-2011, 12:38 AM -
JTextField Question
By Kyle227 in forum New To JavaReplies: 5Last Post: 05-09-2010, 07:44 AM -
JTextField question
By Chasingxsuns in forum New To JavaReplies: 5Last Post: 07-14-2009, 02:39 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks