Results 1 to 12 of 12
- 10-25-2010, 03:49 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Jtextfield value get and calculation
Hello
This my code
I need to calculate tax, multiply by 0.25 and then show the result in the tax text filed.Java Code:<script type="text/javascript"> <!-- public void actionPerformed(ActionEvent event) { double income; double net; //double tax = 0.25; /*String input; input = txt1.getText(); double txt1 = Double.parseDouble(input); input = txt2.getText(); double txt2 = Double.parseDouble(input);*/ //income = txt1 *= 0.25; String s = txt1.getText(); String income = s ; txt2.setText(income ); //income = (txt1 * tax); } } //--> </script>
-
First of all, you need to get your code to compile, and the code listed above won't since you're declaring an income variable twice, once as a double and once as a String.... so you'll want to fix that first.
Once you've done that, please tell us how your program is not working right, errors, lack of functionality, etc,... and any other details about your program that will help us to answer your question.
Edit: what's with the javascript stuff in your post above? Also, when you next post code, you'll want to fix your indentations since the easier it is to read your code, the easier it will be to understand it and help you.
Luck!
- 10-25-2010, 03:57 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
This compiles in my jGrasp.Java Code:<script type="text/javascript"> <!-- class txtHandler implements ActionListener { public void actionPerformed(ActionEvent event) { String s = txt1.getText(); String income = s ; txt2.setText( income); } } //--> </script>
I need to know how to add an operation and to show the result in txt2.
I hope this is clearer.
-
Yes, that compiles. You'll want to parse the String into a double, you have code that does this above, so I assume that you know how to do this, and then take the result from the calculation and place it into txt2. The tricky part will be that you can't place a double value into txt2 but rather a String. The easiest way to convert the double returned from your calculation into a String is to simply use the String.valueOf(doubleVariable) method. If you do that and don't like the appearance of the result, we can discuss better ways of formatting the double value into a String.
Luck!
- 10-25-2010, 04:03 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
could you give an example.
I have a txt1 that is income.
txt2 is tax = 0.25.
I need to multiply the income by 0.25 and show the result in the txt2.
-
Example of what? You haven't told us where your problem is.
I understand, but where are you stuck?I have a txt1 that is income.
txt2 is tax = 0.25.
I need to multiply the income by 0.25 and show the result in the txt2.
Let's go over this step by step:
1) Get the information from txt1. OK, you have done this and placed it into String s (though the variable name should be more descriptive).
2) Translate the String into a double and place into a double variable. Again, you have commented out code that uses Double.parseDouble. Your next step should be to try to do this with your current code by calling Double.parseDouble(s) and put the result into a double variable. Make sure that the double variable has a unique name and is not given the same name as your JTextField.
3) Do your math: this part's trivial and I think that you should be able to do it easily without our help.
4) Take the result and place into the txt2 JTextField. The previous thread that you hijacked has an example of this .... but let's not worry about this until you've done steps 2) and 3).
- 10-25-2010, 04:19 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Sorry could not get it to compile.
The thing teacher asked to to convert the string to inter
int num = Integer.parseInt(s)
- 10-25-2010, 04:20 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
to convert to integer.
-
-
11:43 pm... best of luck and goodnight! :)
- 10-25-2010, 04:47 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Here yo go.
It compiles but the result did not show.
Java Code:<script type="text/javascript"> <!-- class txtHandler implements ActionListener { public void actionPerformed(ActionEvent event) { //String input; double income; //income amount //input = txt1.getText(); String s = txt1.getText(); String tax = s ; income = Double.parseDouble(tax) * 0.25; txt2.setText(tax); } } //--> </script>
-
It's only doing what you are telling it to do:
You are calculating a tax and placing it into the income variable, but then are ignoring this result and do nothing with it. Also you are taking String s, which was pulled from txt1, and then putting it back into txt2 unchanged, again ignoring income, so you really can't expect the program to show a proper result.
Better to change income into a String (I tell you how to do this in my earlier posts) and placing that into txt2.
Anywho, I need my rest, so keep on plugging, and someone else will jump in and help you if you get stuck.
best of luck!
Similar Threads
-
Calculation with char
By chuckbalzer in forum New To JavaReplies: 7Last Post: 09-20-2010, 05:29 PM -
Help with the calculation of a variable.
By rarschach in forum New To JavaReplies: 14Last Post: 01-24-2010, 02:50 PM -
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02-24-2009, 11:44 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
Problem with Calculation ....
By danny000 in forum New To JavaReplies: 1Last Post: 04-15-2008, 02:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks