-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchArray.java
More file actions
29 lines (22 loc) · 788 Bytes
/
SearchArray.java
File metadata and controls
29 lines (22 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
public class SearchArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// int[] numbers = {1, 9, 4, 6, 2, 7, 3};
String[] fruits = {"apple", "orange", "banana"};
boolean isFound = false;
System.out.print("Enter a fruit to search for: ");
String target = scanner.nextLine();
for(int i = 0; i < fruits.length; i++) {
if(target.equals(fruits[i])) {
System.out.println("Element found at index " + i);
isFound = true;
break;
}
}
if(!isFound) {
System.out.println("Element not found in array");
}
scanner.close();
}
}