Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Please tell me why this isn't calculating.

    The assignment I am doing here uses radio buttons to dictate a gas price essentially. So in this instance,

    if the error radio button is selected the input field which is coolBoy will be multiplied by 5.35.
    The only issue is, when I have error radio button selected and enter the input, it produces a 0.0 result EVERY SINGLE TIME.
    coolBoy2 is a non-modifiable JTextField that the result goes onto.

    Please give me the answer. I've gotten help here before but people keep giving me answers that make no sense and it's super confusing. This is a very basic assignment.



    Java Code:
    public void enterJButtonActionPerformed(ActionEvent event)
    	{
    		if(errorJRadioButton.isSelected())
    		{
    			 // error message  <------ IGNORE THIS COMMENT LINE
    			error = Double.parseDouble(coolBoyJTextField.getText());
    		
    		
    		error = coolBoy * 5.35;
          		
    	    		coolBoy2JTextField.setText("" + (error));
    
    		}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    Line 6 and line 9 both assign a value to error. The second assignment will replace the first value.
    What is the value in coolBoy used on line 9?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    coolBoy is the JTextField that catches the input. It is also a double and a JLabel.
    So are you saying that I need another JLabel in place of coolBoy for line 9?
    I.E JTextField JLabel is the gettext JLabel then maybe
    JLabel coolBoy3 is the JLabel that is replaced in line 9?

    So

    error = Double.parseDouble(coolBoyJTextField.getText());


    error = coolBoy3 * 5.35;
    ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    It is also a double and a JLabel.
    Sorry, the compiler only allows ONE variable with a name. There can only be one variable named: coolBoy and I suspect it is a double.

    What is the value in coolBoy when line 9 is executed? If it's value is 0 then 0 times 5.35 is 0 and that is the value that will be assigned to error.

    What happens to the value assigned to error on line 6? Do you want to use that value for anything?
    Because on line 9 the value in error is changed and the value from line 6 is gone.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    I am not sure what you're asking but this is how I am perceiving it.

    Line 6
    error = Double.parseDouble(coolBoyJTextField.getText());

    I am assuming that this says that if error is selected, it takes the input from JTextField coolBoy

    And I am also assuming that if it was not written this way it would not be able to properly

    error = coolBoy3 * 5.35;

    So the value is whatever someone puts in the JTextField coolBoy

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    Ok, lets try with one thing at a time:

    What is the value in coolBoy when line 9 is executed?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    My belief is the value of coolBoy in line 9 correlates to the input of coolBoy on line 6.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    There is no relationship shown in the posted code between the contents of the text field named coolBoyJTextField on line 6
    and the contents of the variable coolBoy used on line 9.

    Is there code somewhere else that takes a value from the text field and saves it in coolBoy?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    You're talking about some stuff that's a little over my head. It was never this difficult before...

    Every assignment was literally about making JLabels and stuff and using those labels for multiplication or addition or division etc.

    I don't know what you're talking about really.

    coolBoy IS THE JTEXTFIELD though.


    JTextField coolBoyJTextField;
    double coolBoy;
    JLabel coolBoyJLabel;


    Are part of the declarations

    but JTextField coolBoy is LITERALLY the only input for the entire program and it is supposed to be modified by whatever JRadio is selected, in this case by 5.35

    What am I not understanding here because I'm starting to become even more confused.
    Last edited by Lawlington; 11-25-2016 at 08:21 PM.

  10. #10
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    Essentially there will be 3 JRadio, 1 JTextField input[coolBoy] then coolBoy2 a result JTextField after the math is done.

    Box 1 selected 5.35 * the input JTextField
    Box 2 4.95 * the input JTextField
    Box 3 3.95 * the input JTextField
    All 3 input JTextField = coolBoy

    Result goes to 2nd JTextField coolBoy2

    Doesn't sound like it should be crazy advanced. I don't get what you're asking to be really honest.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    what you're asking to be really honest.
    I assume that the variable named: coolBoy is a double and has a value. What is the value in coolBoy when line 9 is executed?
    To see what its value is, add this statement just before line 9:
    Java Code:
      System.out.println("coolBoy="+coolBoy); // show the value in coolBoy
    After executing the code, look at the console to see what was printed.
    Last edited by Norm; 11-25-2016 at 08:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    stuff that's a little over my head.
    If talking about variables and the contents of variables is too complicated for you, I suggest that you talk with your instructor about getting the background you need to be able to write programs.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    I have the most useless instructor. Took an online class, BIG mistake because I learn more from interaction. I've sent him emails looking for help and he has not responded. He only has responded to one email. I have gone to tutoring, where I was educated as to how long and how good the two programming tutors were at programming rather than learning a SINGLE thing about java.

    I'm -dead- serious.

    And I appreciate this help. The thing is, it's a little more difficult for me to learn through reading rather than real time conversation.

    Okay so I entered 55 into the input field and it returned this result:

    coolBoy=0.0

    Truth is, I'm pretty proud that I've mostly taught myself 8 chapters of Java. I don't understand the basic concepts of how the brackets work, I understand a little about what the variables are and how if statements work.

    I have an A in this class so far, just the recent chapters are hard.

    Instructor made us buy a book and basically gave us no additional resources and will give example programs in the assignment folders. [I've been using the examples. If it wasn't for them I don't think I'd have gotten so far.]
    Last edited by Lawlington; 11-25-2016 at 08:44 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    coolBoy=0.0
    Ok. That is what I expected.
    If the contents of coolBoy is 0.0 then the value assigned to error will be 0:
    error = coolBoy * 5.35; // 0 times 5.35 is 0

    What is the purpose of the code on line 6?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    error = Double.parseDouble(coolBoyJTextField.getText());

    error is the name of the JRadio. So essentially [I believe] I am stating that
    If error JRadio is selected, parse the double from input coolBoyJTextField [gather the user input]

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    error is the name of the JRadio
    No. It can not be. error must be a double for that statement to compile.

    parse the double from input coolBoyJTextField [gather the user input]
    Ok. Now what is done with the double that was parsed from the String in the coolBoyJTextField text field?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    error is declared as a double, is there anything else that must be done with it?


    This is what is to be done with the input:
    error = coolBoy * 5.35;

    Also, if I was to declare coolBoyTextField as a string, would it conflict with the Double.parseDouble?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    This is what is to be done with the input:
    error = coolBoy * 5.35;
    What is the "input" in that statement?

    If the "input" from line 6 was saved in the variable: error, then that statement replaces that input value with the value of the expression: coolBoy * 5.35
    And since coolBoy has a 0 value, then error gets a 0 value.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this isn't calculating.

    error = error * 5.35; produces the right answer, but it's producing answers like:

    481.49999999999994

    Is there a way to simplify the results to the first two decimals?

    481.49

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this isn't calculating.

    a way to simplify the results to the first two decimals?
    Yes, the contents of a double can be formatted to a String with the desired decimal places using the DecimalFormat class.
    Also the String class has a method that will format data into a String.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Calculating Pi
    By Simon4360 in forum New To Java
    Replies: 3
    Last Post: 03-04-2013, 06:22 PM
  2. calculating the mean
    By jimbao in forum New To Java
    Replies: 8
    Last Post: 12-14-2012, 01:40 AM
  3. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 09:14 PM
  4. calculating the hypotenuse
    By Latanyar in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 09:20 AM
  5. calculating with dates
    By hannes in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 08:22 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
  •