Home > Back-end >  How can I check if an element is found and not found in an array by reading its value (content)
How can I check if an element is found and not found in an array by reading its value (content)

Time:09-16

Question:

Complete the program so that it asks the user for a number to search in the array. If the array contains the given number, the program tells the index containing the number. If the array doesn't contain the given number, the program will advise that the number wasn't found.

My code:

Scanner scanner = new Scanner(System.in);
        int[] array = new int[10];
        array[0] = 6;
        array[1] = 2;
        array[2] = 8;
        array[3] = 1;
        array[4] = 3;
        array[5] = 0;
        array[6] = 9;
        array[7] = 7;

        int searchValueInput = scanner.nextInt();

        for (int loopValue = 0; loopValue < array.length; loopValue  ) {
            if (searchValueInput == array[loopValue]) {
                System.out.println(searchValueInput   " is at index "   loopValue   ".");
            } else if (searchValueInput > array.length) {
                System.out.println(searchValueInput   " was not found.");
            }
        }

I have modified my code to a simpler logic, because I have complicated it a lot. I have also defined the problem and it is as follows:

How can I check if an element value is inside an array (this part is already done I believe)

How can I check if an element value is not found within the array (this is the part I am missing)

The output result should look similar to the following:

Search for? 3
3 is at index 4.

Search for? 9
9 is at index 6.

Search for? 22
22 was not found.

CodePudding user response:

Complete explanation

You should extract searching the array to a method.


import java.util.Scanner;

class Example {
// 1
    public static int search(int[] arr, int target){
        
        // 2 
        for(int i = 0; i < arr.length; i  ){
            // 3
            if(arr[i]==target)
                // 4
                return i;

        }
        // 5
        return -1; 
    }

// 6
    public static void main(String[] args){
    
        int[] arr = {4,3,2,1,6,5,7,8,9,0};
        int index = search(arr, 4);
        if(index >= 0)
            System.out.println("Element found at "   index);
        else 
            System.out.println("Element can't be found");
    }
}
  1. Start the method definition.
  2. Loop through the array, with the index i starting from 0.
  3. Check if arr[i] is equal to the target being searched for.
  4. If true return the index. (Function ends here)
  5. Return -1 if the index can't be found.
  6. Driver code

CodePudding user response:

The problem is that your code does compare between your input value and the value inside the array, such as if the value you input is greater than the value inside the array it spits out "Not found"

I'm not sure if I undestood the problem correctly, try the following code

Scanner scanner = new Scanner(System.in);
int[] array = {6,2,8,1,3,0,9,7,0,0};

System.out.println("Search for? ");
int searchValueInput = scanner.nextInt();
boolean found = false;
for (int loopValue = 0; loopValue < array.length; loopValue  ) {
    if (array[loopValue] == searchValueInput) {
        System.out.println(searchValueInput   " is at index "   loopValue   ".");
        found = true;
        break;
    }
}
if(!found)
    System.out.println(searchValueInput   " was not found.");

Hope this helps

  •  Tags:  
  • java
  • Related