user3105240
user3105240

Reputation: 3

when I return an array, I get a "cannot find symbol" error

I am creating a program where the user (teacher) enters a bunch of percentage grades, and these grades get analyzed, and an outputBox prints out the average, min/max, range, and the number of grades that are 'Level R', 'Level 1', 'Level 2' and so on.

The problem I am facing is the method which sorts the grades into different levels. When I try to return my array to the main method, and try to print it to my output, I get an error for the array, saying "cannot find symbol"

import java.util.Arrays.*;



public Integer[] Levels(){
Integer[] levelCount = {0,0,0,0,0};
int mark = Integer.parseInt(markInput.getText());

int i;
for (i=0; i<levelCount.length;i++ )
{
    if (mark <50){
        levelCount[0] = levelCount[0]+1;
    }
    else if ((mark >= 50) && (mark <60)){
        levelCount[1] = levelCount[1]+1;
    }
    else if ((mark >=60) && (mark<70)){
        levelCount[2] = levelCount[2]+1;
    }
    else if ((mark >=70)&&(mark<80)){
        levelCount[3] = levelCount[3]+1;
    }
    else if ((mark >=80) && (mark<=100)){
        levelCount[4] = levelCount[4]+1;

    }
}

return levelCount;

so, when I go to my main method to print out all the information, I am supposed to put the levelCount array into ANOTHER array, which I did. Here is what I have:

String [] level = {"\nNumber at Level R: " + Levels(levelCount[0]), 
"\nNumber at Level 1: " +Levels(levelCount[1]), 
"\nNumber at Level 2: " + Levels(levelCount[2]), 
"\nNumber at Level 3: " + Levels(levelCount[3]),
"\nNumber at Level 4: " + Levels(levelCount[4])};

So my grand dilemma, is that the array levelCount shows the error 'cannot find symbol'. No matter what I try, I continue getting this error.

I feel like the answer is extremely obvious, but I searched online for something that would help me with this problem, and nothing seems to work, I keep getting the same error.

Upvotes: 0

Views: 1235

Answers (2)

Kejne
Kejne

Reputation: 24

Your method Levels does not take any argument, so when you try to create the String array in the main method, that won't compile. Your Levels method returns String[] as well. Try changing the use of the method like this:

int[] levelCount = Levels();    
String [] level = {"\nNumber at Level R: " + levelCount[0], 
"\nNumber at Level 1: " + levelCount[1], 
"\nNumber at Level 2: " + levelCount[2], 
"\nNumber at Level 3: " + levelCount[3],
"\nNumber at Level 4: " + levelCount[4]};

And change the array to int or Integer as proposed by Asfab.

Upvotes: 0

Asfab
Asfab

Reputation: 386

Change line Integer[] levelCount = {0,0,0,0,0}; to

Integer[] levelCount = {0,0,0,0,0,0};

Or remove levelCount[5] = levelCount[5]+1;

In the above line you are trying to read 6th (index 5) element , but your declared Array size is 5 (i.e max index 4)

Upvotes: 2

Related Questions