Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.opencsv.CSVReader;
- import com.opencsv.CSVReaderBuilder;
- import java.io.*;
- import java.util.*;
- public class Devices {
- public static void main(String[] args) {
- // CSVReader(); //this works just fine
- linear(); //not working
- }
- static void CSVReader() {
- System.out.println("Here are all the devices: ");
- CSVReader reader = null;
- try {
- FileReader filereader = new FileReader("devices.csv");
- CSVReader csvReader = new CSVReaderBuilder(filereader)
- .build();
- List<String[]> lines = csvReader.readAll();
- for (int i = 0; i < lines.size(); i++) {
- String[] lineContents = lines.get(i);
- System.out.println(lineContents[7]);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static <T extends Comparable<T>> boolean linearSearch(T[] data, int min, int max, T target){
- int index = min;
- boolean found = false;
- while (!found && index <= max){
- found = data[index].equals(target);
- index++;
- }
- return found;
- }
- static void linear() {
- System.out.println("Device found with searching method: ");
- String d = "Samsung Galaxy S10 5G";
- CSVReader reader = null;
- try {
- FileReader filereader = new FileReader("devices.csv");
- CSVReader csvReader = new CSVReaderBuilder(filereader)
- .build();
- List<String[]> lines = csvReader.readAll();
- for (int i = 0; i < lines.size(); i++) {
- String[] lineContents = lines.get(i);
- System.out.println(lineContents[7]);
- linearSearch(lines, 1, lines.length, d);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- What device are you interested on?
- Samsung Galaxy S10 5G
- Here is its informaiton
- 1000 Dollars, 256 GB, Released in 2019. Snapdragon 855,....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement