2

I would like to display a message indicating if an input value is present in my rollValue array.

My code does't correctly identify such numbers. This search method is what I have attempted, but why doesn't it display the correct message?

public void search(int value)
{
    for (int i = 0; i < rollValue.length; i++)
    {
        if (rollValue[i] == value)
        {        
            System.out.println(value + " was found");
        }            
        else
        {
            System.out.println(value + " was not found");
        }
        break;
    }
}

2 Answers 2

3

You need to iterate (potentially) every value, before you can decide if it was found. Make two methods. Something like

private boolean hasValue(int value) {
    for (int roll : rollValue) {
        if (value == roll) {
            return true;
        }
    }
    return false;
}

Then you can implement search by calling hasValue like

public void search(int value)
{
    if (hasValue(value)) 
    {        
        System.out.println(value + " was found");
    }
    else
    {
        System.out.println(value + " was not found");
    }
}

or as a one line ternary like

System.out.println(value + (hasValue(value) ? " was found" : " was not found"));

or you could add a short-circuit return in search and do something like

public void search(int value) {
    for (int roll : rollValue) {
        if (roll == value) {
            System.out.printf("%d was found%n", value);
            return;
        }
    }
    System.out.printf("%d was not found%n", value);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your for loop is breaking after the first iteration. Move break inside the if used to find the value, maybe like this:

for (int i = 0; i < rollValue.length; i++)
        {
            if (rollValue[i] == value)
            {        
                System.out.println(value + " was found");
               break;

            }

            else
            {
                System.out.println(value + " was not found");
            }
        }

Also you may not need to print the "was not found" string all the time so take it away.

4 Comments

This is what's happening with that code. 3 was found 2 was not found 2 was not found 2 was not found 2 was not found 2 was found
@MaxLawrence I knew this question is coming ;-) Just use a boolean flag with default value false. If item found, set the flag before breaking the loop. Outside loop, check flag and print accordingly.
Would the boolean be inside the for loop?
@MaxLawrence If i tell u everything you wouldn't be able to learn. So give it logical tries. More you struggle, longer will u remember stuff .Good luck!

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.