-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchArrayList.java
43 lines (38 loc) · 1.38 KB
/
BinarySearchArrayList.java
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
36
37
38
39
40
41
42
43
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package exercises;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* @BingChat
* @author MOPHE
*/
public class BinarySearchArrayList {
public static void main(String[] args) {
String path = "C:\\Users\\MOPHE\\Documents\\NetBeansProjects\\Exercises\\src\\exercises\\files\\english.txt";
ArrayList<String> list = new ArrayList<>();
try {
File file = new File(path);
try (Scanner scanner = new Scanner(file)) {
scanner.useDelimiter("\n");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
list.add(line);
}
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
}
Collections.sort(list);
System.out.println(list);
Scanner sc = new Scanner(System.in);
String input = sc.next();
int index = Collections.binarySearch(list, input);
System.out.println(input + " is found at index " + index);
}
}