-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRange.java
More file actions
35 lines (28 loc) · 1.26 KB
/
Copy pathSearchInRange.java
File metadata and controls
35 lines (28 loc) · 1.26 KB
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
30
31
32
33
34
35
package Searching;
import java.util.Arrays;
import java.util.Scanner;
public class SearchInRange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] fruits = {"Apple", "Banana", "Mango", "Orange", "Pineapple", "Grapes",
"Papaya", "Guava", "Watermelon", "Strawberry", "Blueberry", "Raspberry",
"Cherry", "Peach", "Pear", "Plum", "Kiwi", "Lemon", "Lime", "Pomegranate",
"Coconut", "Apricot", "Fig", "Dragon fruit", "Lychee"};
System.out.println(Arrays.toString(fruits));
System.out.print("Enter the target word : ");
String target = in.nextLine();
System.out.print("Enter the range to search element from (st end) : ");
int index1 = in.nextInt();
int index2 = in.nextInt();
System.out.println(SearchFruit(fruits , target , index1 , index2));
}
static String SearchFruit(String[] arr , String target ,int st , int end){
if (st < 0 || end >= arr.length || st > end)
return "invalid range!";
for(int i = st ; i <= end ; i++){
if (arr[i].equalsIgnoreCase(target))
return "Found at index : "+ i;
}
return "Doesn't exist in the given range!";
}
}