I want to return the value of i, but I don't know how. My code is:

public int indexOfFile(String filename) {
  int value=0; 
  for(int i=0; i<DRIVE_SIZE; i++) {
    if(drive[i]!=null && drive[i].getName().equals(filename)) {
      value= i; 
    }else{
      value= -1; 
    }
  }
  return value; 
}

so I want the value of i, but no matter what I do, all I get is -1. Can someone show me what I'm doing wrong?

you're continuing to run the loop after you've found it, which means you'll come into your 'else' block again. and there you overwrite the value you have stored.

I think, a better way would be to run your loop, when you've found your desired index, don't store it in a value, just immediately return it.
and after the loop, just put return -1, this way, if your index is not found, you'll still return -1.

You loop through all the files in drive[]. When you hit the right one you set value = i, which is the index you want. But then you continue through the rest of the array, and set value = -1 overwriting the value you wanted (unless the one you wanted happened to be the last file in the array).
As soon as you hit the required file you should return immediately, as in

if(drive[i]!=null ....)  return i;

then after the loop, if you get that far you konw you didn't find the file so you can

return -1;

Hi stultuske. My turn to post second this time!
:-0
J

Now I have:

public int indexOfFile(String filename) { 
  int indexOfFile=0; 
  for(int i=0; i<DRIVE_SIZE; i++) {
    indexOfFile=0; 
    if(drive[i]!=null && drive[i].getName().equals(filename)) {
      return i;
    }else{
      return -1;      
    }
  } 
}

But it says that I'm missing a return statement. Now what do I do?

forget the else and move the return -1 out of the for loop.

Not quite right. What happens if the correct file is in drive[1]? - it's not in drive[0] so you immediately return -1 and that's it.
You get the error message because Java sees that if you complete the loop you will go from line 9 to line 10 and exit the method without returning anything. You know that that's not possible with your code, but Java doesn't.

Time to go back and read the previous posts with the same care that they were written with. You just had two independent posts that both said "after the loop ... return -1;"

It still doesn't work. Now I have:

public int indexOfFile(String filename) { 
  int indexOfFile=0; 
  for(int i=0; i<DRIVE_SIZE; i++) {
    indexOfFile=0; 
    if(drive[i]!=null && drive[i].getName().equals(filename)) {
      return i;
    }
  }
      return -1;       
}

And it still returns -1

Lines 2 and 4 are redundant - you never use the value of int indexOfFile.

The structure looks OK now, so you should use print statements to check the data values. eg instead of line 4 try
System.out.println(drive.getName());
so you can see what's actually happening in your loop.

also, make sure you take everything in account: case sensitivity and all ..
do as JamesCherrill suggested, and also print the value your testing against: filename, that should give you everything you need.

Not quite right. What happens if the correct file is in drive[1]? - it's not in drive[0] so you immediately return -1 and that's it.

I assume that wasn't a response on my answer? :)

I assume that wasn't a response on my answer? :)

No, it was a comment on the previous post with the OP's code. I've never known your posts to be "not quite right"! ;-)

maybe you haven't read all my posts yet :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.