0

I have the code:

for (int i = 0; i < directory.length; i++) {
    if (directory[i].contains (number)) {
        if (directory[i+1] == null){
            directory[i] = null;
        }
        else {
            for (int k=i; k<directory.length; k++){
                System.out.println(directory[k]);
                System.out.println(directory[k+1]);
                if (directory[i+1] != null){
                    directory[k] = directory[k+1];
                    System.out.println(directory[k]);
                }

The important bit i think is "if (directory[i+1] != null){" what i am using is an array and i am trying to find if the next element is not null then do the code. However, even if the next value is null it does it anyway. Any suggestions?

Thank you.

4
  • This is an i-versus-k typo issue. Commented Feb 27, 2014 at 20:53
  • Sorry for not making the question clear, I have fixed the question now :) Commented Feb 27, 2014 at 20:55
  • 4
    I think you'd get an array index out of range error on the last iteration of the i loop, no? Commented Feb 27, 2014 at 21:00
  • What is the code supposed to do, and what is it not doing as expected? Commented Feb 27, 2014 at 21:04

2 Answers 2

1

Your problem is that you are not using the loop-index in your loop. You're only testing the initial value, over and over again.

This

if (directory[i+1] != null){

should be this

if (directory[k+1] != null){
Sign up to request clarification or add additional context in comments.

Comments

1

the next value of directory[i+1] is never going to be null @"if (directory[i+1] != null){" because before the else statment you check to see if directory[i+1] == null

my guess is a small typo in iteration maybe you ment directory[k+1]?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.