Results 1 to 7 of 7
  1. #1
    nksjolinder is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default I cant figure out why this variable isn't recognized...

    Java Code:
    if (weekTotal1 > 40 && weekTotal2 > 40){
            double overtime = ((weekTotal1 + weekTotal2) - 80) * 22.50;
        jTextField36.setText(String.valueOf(weekTotal1 + weekTotal2));
        }
        
        if (weekTotal1 > 40 && weekTotal2 <= 40){
        jTextField36.setText(String.valueOf([COLOR="#FF0000"]overtime[/COLOR]));
        }
    I do not know why "overtime" is not being recognized within the if statement. Please help!
    Last edited by sunde887; 09-26-2011 at 05:47 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: I cant figure out why this variable isn't recognized...

    You declare it inside the scope of one if statement, so it's only available inside that particular if statement. Declare it in a wider scope (outside the if statement) to have access to it in both if statements.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default Re: I cant figure out why this variable isn't recognized...

    Because it's declared inside one if statement which, depending on the values of weekTotal1 and weekTotal1, might never be reached. Therefore in the second if statement it does not recognize it?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: I cant figure out why this variable isn't recognized...

    Quote Originally Posted by TaxpayersMoney View Post
    Because it's declared inside one if statement which, depending on the values of weekTotal1 and weekTotal1, might never be reached. Therefore in the second if statement it does not recognize it?
    It doesn't matter whether it's reached or not. Even if the declaration is reached, it will still go out of scope at end of that if statement.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    nksjolinder is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: I cant figure out why this variable isn't recognized...

    Is there any way i can apply those same "if conditions" to the variable without declaring it inside an if statement?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: I cant figure out why this variable isn't recognized...

    What should the value of overtime be in the second if statement?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    nksjolinder is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: I cant figure out why this variable isn't recognized...

    I may have fixed it...here is the updated code

    if (weekTotal1 > 40 && weekTotal2 > 40){
    double overtime = ((weekTotal1 + weekTotal2) - 80) * 22.50;
    jTextField36.setText(String.valueOf(overtime));
    }

    if (weekTotal1 > 40 && weekTotal2 <= 40){
    double overtime = (weekTotal1 - 40) * 22.50;
    jTextField36.setText(String.valueOf(overtime));
    }

    if (weekTotal1 <= 40 && weekTotal2 > 40){
    double overtime = (weekTotal2 - 40) * 22.50;
    jTextField36.setText(String.valueOf(overtime));

Similar Threads

  1. .Jar Not Recognized
    By Java Tyler in forum New To Java
    Replies: 11
    Last Post: 08-12-2010, 04:36 PM
  2. Javac not recognized
    By OscarLiu in forum New To Java
    Replies: 8
    Last Post: 06-29-2010, 04:10 PM
  3. 'java is not recognized' vista
    By dylan mc in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 06:54 PM
  4. Javac is not recognized?
    By Java idiot in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 07:54 PM
  5. InFile Commands not being recognized.
    By Tenn in forum New To Java
    Replies: 4
    Last Post: 02-13-2009, 04:50 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
  •