0

I have developed the following for loop to set the data in a drop down list. This works perfectly if I use a number on the .get() method to select and compare which item has been clicked, but obviously this is useless with a set integer value.

I'm getting the error that the 'a' variable cannot be resolved to a variable.

I'm really not sure why though?

Here's the code:

List<String> list = new ArrayList<String>();
list.add("-");
list.add("Medical");
list.add("Business");
list.add("Family");
list.add("Other");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
editTime.setAdapter(dataAdapter);

for(int a = 0; a < list.size(); a ++);
{
    // Error on the 'a' variable - cannot be resolved to a variable.    
    if(typeReturned == list.get(a));
    {
        // Error on the 'a' variable - cannot be resolved to a variable.
        editTime.setSelection(a);

    }
}
TN888
  • 7,659
  • 9
  • 48
  • 84
user1352057
  • 3,162
  • 9
  • 51
  • 117

9 Answers9

8

You have an unnecessary semicolon after your for loop:

for(int a = 0; a < list.size(); a ++);

If you don't delete it, it is the same as writing:

for(int a = 0; a < list.size(); a ++) { }
// a isn't in scope here any more.

You have the same mistake after your if condition. You have to get rid of that semicolon, too.

Also, I think typeReturned is a String, and you are comparing it with ==. That is a bad idea. You have to use .equals(), if you want to check if both strings have the same content, see:

How do I compare Strings in Java

Community
  • 1
  • 1
jlordo
  • 37,490
  • 6
  • 58
  • 83
4

Remove the semi colon at the end of your for loop

Renjith
  • 3,274
  • 19
  • 39
4

You have a spare semi-colon at then end of your for loop. Removing that should fix the problem.

Tim
  • 248
  • 3
  • 14
4
for(int a = 0; a < list.size(); a ++);

Remove the ; at the end.

Grambot
  • 4,370
  • 5
  • 28
  • 43
4

You must delete ";" after forand after if.

Your improved code :

    for(int a = 0; a < list.size(); a ++) //WITHOUT SEMICOLON !
    {

    if(typeReturned == list.get(a)) //WITHOUT SEMICOLON !
    {

         editTime.setSelection(a);
    }

    }

Now should be ok.

TN888
  • 7,659
  • 9
  • 48
  • 84
3

You have two semicolon after for

for(int a = 0; a < list.size(); a ++);

Also after if

if(typeReturned == list.get(a));

Remove both and it will work

Android Killer
  • 18,174
  • 13
  • 67
  • 90
2

Remove the semicolons after if and for statements.

for(int a = 0; a < list.size(); a ++);   
if(typeReturned == list.get(a));

for loop and if statements are not complete statements to put a semicolon after them. They are conditional statements to check for and to execute the block or line after them.

sr01853
  • 6,043
  • 1
  • 19
  • 39
0

You have just syntax errors in your code as mentioned by others as well.

A semicolon in Java denotes the end of statement so your for loop ends where it encounters the first semicolon regardless of the curly braces afterwards. The rest of the code is considered as a separate block and the variable 'a' is local only the the for loop so is not visible after the first semicolon.

The same applies to the semicolon after the if statement.

for(int a = 0; a < list.size(); a ++);  //** REMOVE THIS SEMICOLON
    {

    // Error on the 'a' variable - cannot be resolved to a variable.    
    if(typeReturned == list.get(a));  //** REMOVE THIS SEMICOLON
    {
        // Error on the 'a' variable - cannot be resolved to a variable.
        editTime.setSelection(a);

    }

    }

Hope this helps.

LiaqatG
  • 367
  • 1
  • 3
  • 17
  • My friend, this is just a syntax error, nothing wrong with the logic. Yes, you may have a different approach as how to do a specific task to get the same desired result and that will be your 'logic', but having an extra **semicolon, comma, dot,** etc. are part of the syntax of a language. – LiaqatG Jan 30 '13 at 14:53
0

Remove semi colons from if and for statements.

NoviceDeveloper
  • 1,270
  • 3
  • 15
  • 41