Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.*;
  4.  
  5.  
  6.  
  7. public class Upg1_7
  8. {
  9. //region Variabels
  10. Scanner fileScanner;
  11. File file;
  12. List<String> dat;
  13. //endregion
  14.  
  15. public Upg1_7(boolean localFile, String dir)
  16. {
  17. if (localFile) file = new File(System.getProperty("user.dir") + "\\" + dir);
  18. else file = new File(dir);
  19.  
  20. try
  21. {
  22. fileScanner = new Scanner(file);
  23. dat = new ArrayList<>();
  24. } catch (FileNotFoundException e)
  25. {
  26. System.out.println("File not found");
  27. e.printStackTrace();
  28. return;
  29. }
  30.  
  31. while (fileScanner.hasNext())
  32. {
  33. dat.add(fileScanner.next());
  34.  
  35. }
  36.  
  37. }
  38.  
  39. public static String findName(ArrayList list, int index)
  40. {
  41. return (String) list.get(index);
  42. }
  43.  
  44. public static boolean deleteName(ArrayList list, String name)
  45. {
  46. try
  47. {
  48. Iterator<String> listIter = list.listIterator();
  49. int index = 0;
  50. while (listIter.hasNext())
  51. {
  52. if (name.equals(listIter.next()))
  53. {
  54. list.set(index, null);
  55. return true;
  56. }
  57. index++;
  58.  
  59. }
  60. return false;
  61. } catch (Exception e)
  62. {
  63. e.printStackTrace();
  64. throw e;
  65. }
  66. }
  67.  
  68. static int getIndex(ArrayList list, String name)
  69.  
  70. {
  71. try
  72. {
  73. Iterator<String> listIter = list.listIterator();
  74. int index = 0;
  75. while (listIter.hasNext())
  76. {
  77. if (name.equals(listIter.next())) return index;
  78. {
  79. list.set(index, null);
  80.  
  81. }
  82. index++;
  83.  
  84. }
  85. return -1;
  86. } catch (Exception e)
  87. {
  88. e.printStackTrace();
  89. throw e;
  90. }
  91. }
  92.  
  93.  
  94. public static boolean addToList(ArrayList list, String name)
  95. {
  96. int index = getIndex(list, name);
  97. if (index != -1)
  98. {
  99. return false;
  100. }
  101. else
  102. {
  103. list.add(name);
  104. Collections.sort(list);
  105. return true;
  106.  
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement