Results 1 to 12 of 12
  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default Jtextfield value get and calculation

    Hello
    This my code
    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>
    I need to calculate tax, multiply by 0.25 and then show the result in the tax text filed.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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>
    This compiles in my jGrasp.
    I need to know how to add an operation and to show the result in txt2.
    I hope this is clearer.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  5. #5
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by globo View Post
    could you give an example.
    Example of what? You haven't told us where your problem is.

    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.
    I understand, but where are you stuck?

    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).

  7. #7
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Sorry could not get it to compile.
    The thing teacher asked to to convert the string to inter
    int num = Integer.parseInt(s)

  8. #8
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    to convert to integer.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by globo View Post
    Sorry could not get it to compile.
    No need to apologize, but we can't help you get it to compile if we don't see your attempt.



    The thing teacher asked to to convert the string to inter
    int num = Integer.parseInt(s)
    OK, then please use this with my blessings. :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    11:43 pm... best of luck and goodnight! :)

  11. #11
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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>

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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

  1. Calculation with char
    By chuckbalzer in forum New To Java
    Replies: 7
    Last Post: 09-20-2010, 05:29 PM
  2. Help with the calculation of a variable.
    By rarschach in forum New To Java
    Replies: 14
    Last Post: 01-24-2010, 02:50 PM
  3. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-24-2009, 11:44 PM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •