Using an if statement with an array

edited November 2013 in Questions about Code

Hello,

I'm rather new to coding, and I'm having a few problems with some of my code. I have an array called array1 that has many empty parts. So I used the sort() method to put those first in the array so that I could make a for loop that found the last index where array1[i] was empty. This is what I wrote:

for (int i = 0; i < array1.length; i++);
{
  if (array1[i].equals(""))
  {
    lastBlank = i;
  }
}

The program keeps telling me that it "cannot find anything named 'i,' though. Any ideas about what I'm doing wrong?

Tagged:

Answers

  • You should aim to post a running code! [-X

  • _vk_vk
    edited November 2013 Answer ✓

    the ; in line one... It is "closing" the block, so i is out of scope when you try to read it. Just remove this entry

  • edited November 2013

    here like _vk said

    String [] array1 = { 
      "s", "e", "q", "", "ws", "dd"
    };
    
    int lastBlank=0;
    
    for (int i = 0; i < array1.length; i++)
    {
      if (array1[i].equals(""))
      {
        lastBlank = i;
      }
    }
    
    print (lastBlank);
    print (" in ");
    for (int i = 0; i < array1.length; i++)
      print (" >" + array1 [i] + "<, ");
    
  • Thank you all so much. Just a stupid typo!

Sign In or Register to comment.