Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3.         ArrayList<Integer> al1 = new ArrayList<>();
  4.         ArrayList<Integer> al2 = new ArrayList<>();
  5.         List<Integer> al3 = new ArrayList<>();
  6.  
  7.  
  8.         String filename = "";
  9.         int somenum = 5;
  10.         int a = 0;
  11.         int zad3 = 0;
  12.         int choice = 0;
  13.         Scanner sc = new Scanner(System.in);
  14.         String str;
  15.         System.out.println("Введите путь:");
  16.  
  17.  
  18.         if (sc.hasNextLine()) {
  19.             filename = sc.nextLine();
  20.         } else {
  21.             System.out.println("Введено неверное значение");
  22.         }
  23.  
  24.         System.out.println("Записать значения в перпвый массив или второй?");
  25.  
  26.         if (sc.hasNextInt())
  27.             choice = sc.nextInt();
  28.         else
  29.             System.out.println("введено неверное значение");
  30.  
  31.         File f = new File(filename);
  32.         StringBuilder sb = new StringBuilder();
  33.         if (f.exists()) {
  34.             try {
  35.                 try (BufferedReader br = new BufferedReader(new FileReader(f.getAbsoluteFile()))) {
  36.                     String s;
  37.                     while ((s = br.readLine()) != null) {
  38.                         String[] strs = s.split(" ");
  39.                         for (String str1 : strs) {
  40.                             str = str1;
  41.                             al3.add(Integer.valueOf(str));
  42.                         }
  43.                     }
  44.                 }
  45.             } catch (IOException e) {
  46.                 throw new RuntimeException();
  47.             }
  48.         }
  49.  
  50.  
  51.         if (choice == 1) {
  52.             for (int i = 0; i < al3.size(); i++) {
  53.                 if (al3.get(i) == 1) {
  54.                     al1.add(i, al3.get(i));
  55.                 }
  56.             }
  57.  
  58.             for (int i = 0; i < al3.size(); i++) {
  59.                 if (al3.get(i) == 0) {
  60.                     al1.add(i, al3.get(i));
  61.                 }
  62.             }
  63.         }
  64.  
  65.         if (choice == 2) {
  66.             for (int i = 0; i < al1.size(); i++) {
  67.                 if (al1.get(i) == 1) {
  68.                     al2.add(i, al1.get(i));
  69.                 }
  70.             }
  71.  
  72.             for (int i = 0; i < al3.size(); i++) {
  73.                 if (al3.get(i) == 0) {
  74.                     al2.add(i, al3.get(i));
  75.                 }
  76.             }
  77.         }
  78.  
  79.  
  80.         try {
  81.             if (!f.exists()) f.createNewFile();
  82.             try (PrintWriter pw = new PrintWriter(f.getAbsoluteFile())) {
  83.                 if (choice == 1) {
  84.                     pw.print(al1);
  85.                 }
  86.                 if (choice == 2) {
  87.                     pw.print(al2);
  88.                 }
  89.             }
  90.         } catch (IOException e) {
  91.             throw new RuntimeException();
  92.         }
  93.  
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement