Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import com.opencsv.CSVReader;
  2. import com.opencsv.CSVReaderBuilder;
  3. import java.io.*;
  4. import java.util.*;
  5. public class Devices {
  6. public static void main(String[] args) {
  7. // CSVReader(); //this works just fine
  8.  
  9. linear(); //not working
  10. }
  11.  
  12. static void CSVReader() {
  13. System.out.println("Here are all the devices: ");
  14. CSVReader reader = null;
  15. try {
  16. FileReader filereader = new FileReader("devices.csv");
  17. CSVReader csvReader = new CSVReaderBuilder(filereader)
  18. .build();
  19. List<String[]> lines = csvReader.readAll();
  20. for (int i = 0; i < lines.size(); i++) {
  21. String[] lineContents = lines.get(i);
  22. System.out.println(lineContents[7]);
  23. }
  24. } catch (FileNotFoundException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. } finally {
  29. if (reader != null) {
  30. try {
  31. reader.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }
  38.  
  39.  
  40. public static <T extends Comparable<T>> boolean linearSearch(T[] data, int min, int max, T target){
  41. int index = min;
  42. boolean found = false;
  43.  
  44. while (!found && index <= max){
  45. found = data[index].equals(target);
  46. index++;
  47. }
  48. return found;
  49. }
  50.  
  51. static void linear() {
  52. System.out.println("Device found with searching method: ");
  53. String d = "Samsung Galaxy S10 5G";
  54. CSVReader reader = null;
  55. try {
  56. FileReader filereader = new FileReader("devices.csv");
  57. CSVReader csvReader = new CSVReaderBuilder(filereader)
  58. .build();
  59. List<String[]> lines = csvReader.readAll();
  60. for (int i = 0; i < lines.size(); i++) {
  61. String[] lineContents = lines.get(i);
  62. System.out.println(lineContents[7]);
  63. linearSearch(lines, 1, lines.length, d);
  64. }
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. } finally {
  70. if (reader != null) {
  71. try {
  72. reader.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
  81. What device are you interested on?
  82. Samsung Galaxy S10 5G
  83. Here is its informaiton
  84. 1000 Dollars, 256 GB, Released in 2019. Snapdragon 855,....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement