How can you assign a variable that is defined inside an if else statement

I need to create something that will find the current hour in GMT and convert it to EST.

When I try to compile and run the program, I get this error: currentHourEST cannot be resolved to a variable

. I think my problem at some point in the statements if else

is that I have assigned the variables incorrectly or something.

// Obtain total milliseconds since midnight, Jan 1, 1970
long totalMilliseconds = System.currentTimeMillis(); 

// Seconds
long totalSeconds = totalMilliseconds / 1000;  
long currentSecond = totalSeconds % 60;

// Minutes
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;

// Hours
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24; 

// Read in EST offset
long offSetAdded = currentHour - 5;

// If the time is negative make it a positive
if (offSetAdded > 0) {
 long currentHourEST = offSetAdded * -1;
} else {
 long currentHourEST = offSetAdded;
}

// Display time
System.out.println("The current time is " + currentHourEST + ":" + currentMinute + ":" + currentSecond);

System.out.println("Current time in GMT is " + currentHour + ":" + currentMinute + ":" + currentSecond);

      

I use an if else statement to plural offSetAdded

by -1

, so that the hour if negative, when I subtract from it 5

, it becomes positive, making it easier for people to see the hour.If the value is offSetAdded

positive then it will be printed currentHour

, just subtracted by 5.

+3


source to share


4 answers


The variable that is defined in the block if

is limited to this if block

, you simply cannot use the variable outside of the if block.

If you want the variable to be used outside the if, just declare it outside the if block.



// If the time is negative make it a positive
long currentHourEST;
if (offSetAdded > 0) {
 currentHourEST = offSetAdded * -1;
} else {
 currentHourEST = offSetAdded;
}

      

+7


source


Change your code to:

// If the time is negative make it a positive
long currentHourEST;
if (offSetAdded > 0) {
    currentHourEST = offSetAdded * -1;
} else {
    currentHourEST = offSetAdded;
}

      

This will declare the currentHourEST

outsite block variable if/else

, so you can use it in the rest of the method code.



Your current code is declaring a variable inside this block, which means that it expires if a block exists in the program if/else

. Therefore, you could not access it later.

Read this variable scoping tutorial to learn more about it.

+6


source


I'm not an expert enough to tell what's going on, but if you declare currentHourEST outside of the if statement and if it's an else statement, it should work. Like this:

long currentHourEST;

// If the time is negative make it a positive
    if (offSetAdded > 0) {
         currentHourEST = offSetAdded * -1;
    } else {
        currentHourEST = offSetAdded;
    }

      

+4


source


First you declare a variable and then you initialize it like

long currentHourEST;
if (offSetAdded > 0) {
  currentHourEST = offSetAdded * -1;
} else {
  currentHourEST = offSetAdded;
}

      

or you can use the conditional operator ? :

(aka ternary)

long currentHourEST = (offSetAdded > 0) ? offSetAdded * -1 : offSetAdded;

      

+2


source







All Articles