1,105,406 Community Members

How do you return the value of an index

Member Avatar
slidepuppy1
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
stultuske
Posting Expert
5,292 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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;
Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
slidepuppy1
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
stultuske
Posting Expert
5,292 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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;"

Member Avatar
slidepuppy1
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
stultuske
Posting Expert
5,292 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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.

Member Avatar
stultuske
Posting Expert
5,292 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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? :)

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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"! ;-)

Member Avatar
stultuske
Posting Expert
5,292 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: