0

This is the code I have come up with. However, I wanted to be able to output either:

  • (value) is in slot x.
  • (value) is in slot x.

two outputs if the (value) is available in two slots -like 7.

or

  • (num) is not in the array.

But not both. Can anyone help please?

    public static void main(String[] args) {
    int search, counter;
    int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};

    System.out.print("Array: ");
    for (int count=0; count<num.length; count++)
        System.out.print(+num[count] + " ");

    Scanner in = new Scanner (System.in);
    System.out.print("\nValue to find: ");
    search = in.nextInt();

    for (counter = 0; counter < num.length; counter++ ){
        if (num[counter] == search)
        {
            System.out.println(search + " is in slot " + (counter + 1) + ".");
        }           
    }
    if (counter == num.length)
        {
            System.out.println(search + " is not in the array.");
        }
}
}

2 Answers 2

1

While I feel like you should probably ask such a question on another community such as https://codereview.stackexchange.com/ I can offer a suggestion:

Use a boolean flag to check whether you have found it before. Something like this:

public static void main(String[] args) {
  int search;
  boolean found = false;
  int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};

  System.out.print("Array: ");
  for (int count=0; count<num.length; count++)
      System.out.print(+num[count] + " ");

  Scanner in = new Scanner (System.in);
  System.out.print("\nValue to find: ");
  search = in.nextInt();

  for (int counter = 0; counter < num.length; counter++ ) {
      if (num[counter] == search)
      {
        System.out.println(search + " is in slot " + (counter + 1) + ".");
        found = true;
      }           
  }

  if (!found) {
        System.out.println(search + " is not in the array.");
  }

  in.close();

}

So you only print the "not found" message when you cannot find the element after a linear traversal through the array...

Sign up to request clarification or add additional context in comments.

1 Comment

suppose using a Boolean flag is the most suitable work around.
0

The problem with your code is that you check if the counter has reached the arrays length. Something that will always happen. You should check if you found a value.

This should do the trick:

public static void main(String[] args) {
    int search, counter;
    int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};
    boolean wasFound = false;

    System.out.print("Array: ");
    for (int count=0; count<num.length; count++)
        System.out.print(+num[count] + " ");

    Scanner in = new Scanner (System.in);
    System.out.print("\nValue to find: ");
    search = in.nextInt();

    for (counter = 0; counter < num.length; counter++ ){
        if (num[counter] == search)
        {
            System.out.println(search + " is in slot " + (counter + 1) + ".");
            wasFound = true;
        }
    }
    if (!wasFound)
    {
        System.out.println(search + " is not in the array.");
    }
}

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.