user1914491
user1914491

Reputation: 89

Why am I getting a varriable not declared error?

I keep getting this error cannot find symbol - variable minDist even though I know it has been declared and initialized. I feel like it's staring me straight in the face. Does anyone know why this is happening?

There's another class file that goes along with this, but I don't think the error is in there.

I get it on the third to last line, when I get to minDist, but if I remove minDist I also get it on minCost and minMPG as well.

public class AnnualFuelUseTester
{
    public static void main(String[] args)
    {
    int sMiles1, sMiles2, sMiles3, sMiles4;
    int eMiles1, eMiles2, eMiles3, eMiles4;
    int[] dist = new int[4];
    double gals1, gals2, gals3, gals4;
    double[] MPG = new double[4];
    double price1, price2, price3, price4;
    double[] cost = new double[4];

    AnnualFuelUse[] fillUps = {new AnnualFuelUse(108438, 108725, 13.9, 2.98),
                               new AnnualFuelUse(108738, 109023, 15.3, 3.02),
                               new AnnualFuelUse(109023, 109232, 10.3, 3.05),
                               new AnnualFuelUse(109564, 109854, 13.1, 3.03)};

    for(int i = 0; i < fillUps.length; i++)
    {
        dist[i] = fillUps[i].calcDistance();
        MPG[i] = fillUps[i].calcMPG();
        cost[i] = fillUps[i].calcCost();
    }
    for (int i = 0; i < dist.length; i++)
    {
        int maxDist = 0;
        int minDist = dist[0];
        if (dist[i] > maxDist)
        {
            maxDist = dist[i];
        }
        if (dist[i] < minDist)
        {
            minDist = dist[i];
        }
    }
    for (int i = 0; i < dist.length; i++)
    {
        double maxMPG = 0;
        double minMPG = MPG[0];
        if (MPG[i] > maxMPG)
        {
            maxMPG = MPG[i];
        }
        if (MPG[i] < minMPG)
        {
            minMPG = MPG[i];
        }
    }
    for (int i = 0; i < dist.length; i++)
    {
        double maxCost = 0;
        double minCost = cost[0];
        if (cost[i] > maxCost)
        {
            maxCost = cost[i];
        }
        if (cost[i] < minCost)
        {
            minCost = dist[i];
        }
    }

    System.out.printf("%15s%15s%15s%15s%15s%15s%15s%15s%15s\n\n"
                       ,"Fill Up", "Days", "Start Miles", "End Miles"
                       ,"Distance", "Gallons Used", "MPG", "Price", "Cost");
    for(int i = 0; i < fillUps.length; i++)
    {
        System.out.printf("%15s%15s%15s%15s%15s%15s%15.2f%15s%15.2f\n"
                          ,(i+1),(int)(1 + i *(i*1.1)), fillUps[i].getmySMiles()
                          ,fillUps[i].getmyEMiles(), dist[i]
                          ,fillUps[i].getmyGals(), MPG[i]
                          ,fillUps[i].getmyPrice(), cost[i]);
    }
    System.out.printf("%10s%10s%30s%30s","Minimum",minDist,minMPG,minCost);
}                        
}

Upvotes: 0

Views: 176

Answers (5)

marc wellman
marc wellman

Reputation: 5886

Always consider the scope in which you are declaring your variables because it determines the visibility of your variable.

You declare your variable within a for-block which is a scope. Then your are trying to reference these variable from outside the scope where you have declared them. That won't work.

public void foo () {

    while (someBool) {

        int someVariable = 0;

        someVariable = 1  // works because using and declaring takes place in the same scope.

    }

 someVariable = 2; // that won't work because variable is not existent in this scope.

}

Also consider that scopes can be hierarchically structured meaning a variable declared in some scope is also visible within all nested scopes:

public void foo () {

    while (someBool) {

        int aVariable = 0;

        if (anotherBool) {

            aVariable = 1; // works because this scope is a nested scope inside the scope where the variable has been declared.

        }
    }
}

You will find plenty of information about the well known concept of scopes which is used not only in C# but in most programming languages.

A point to start you research might be the MSDN documentation:

http://msdn.microsoft.com/en-us/library/aa691132(v=vs.71).aspx

Upvotes: 2

Luke
Luke

Reputation: 5076

Basically, since you said

int minDist = dist[0];

in your for loop, it only exists in your loop. For example,

for(int i = 0; i < 10; i++) {
    int x = 0;
}
System.out.println(x);

would return an error as x isn't there outside of that loop. It's known as scope and is basically the different levels of invisibility. Think like inception, the movie - the people in the 2nd dream level know what the dream in the 1st level is, but the 1st level can't see the 2nd level. Therefore :

int x = 5;
for(int i = 0; i < 1; i++) {
   x = 3; 
   int y = 10;
}
System.out.println(x);
System.out.println(y);

will print out 3 successfully but crash when it tries to print out y because simply put, they can't see y outside of the for loop.

To fix your problem: simply declare minDist outside of your loop, somewhere near the start and it should work.

Upvotes: 0

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

you declared minDist variable inside of loop so scope of that variable limited to that particular for loop.

so you cant access that variable outside.

Upvotes: 1

Thilo
Thilo

Reputation: 262484

You declared minDist inside of a for loop, so it only exists in there, and you cannot use it outside of the loop.

Upvotes: 5

TimoteeTheCodeMonkee
TimoteeTheCodeMonkee

Reputation: 332

You're declaring it within the scope of the for loop. You need to move the declaration of int minDist outside of that loop, to the same level that you're doing your printf.

Upvotes: 4

Related Questions