Advertisement
nikitaxe132

Untitled

Oct 29th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package com.company;
  2.  
  3. import sun.awt.OSInfo;
  4.  
  5. import java.util.Scanner;
  6. import java.io.BufferedReader;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.FileNotFoundException;
  10. import java.io.File;
  11. import java.io.PrintWriter;
  12.  
  13. public class Main {
  14.     static boolean enter(int temp, int max, int min) {
  15.         Scanner scanner = new Scanner(System.in);
  16.         boolean iscorrect = true;
  17.         do {
  18.             try {
  19.                 if ((temp > min - 1) && (temp < max + 1)) {
  20.                     iscorrect = false;
  21.                 } else {
  22.                     System.out.println("This is a mistake. Please enter again!");
  23.                 }
  24.             } catch (Exception e) {
  25.                 scanner.nextLine();
  26.                 System.out.println("This is a mistake. Please enter again!");
  27.                 iscorrect = true;
  28.             }
  29.         }
  30.         while (iscorrect);
  31.         return iscorrect;
  32.     }
  33.  
  34.     static int numb() {
  35.         boolean invalidinput = false;
  36.         int n = 0;
  37.         do {
  38.             boolean iscorrect = false;
  39.             try {
  40.                 BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
  41.                 String text = input.readLine();
  42.                 do {
  43.                     System.out.print("Enter the number of sequence members ");
  44.                     n = Integer.parseInt(text);
  45.                     System.out.println(n);
  46.                     iscorrect = enter(n, 10, 1);
  47.                 } while (iscorrect);
  48.             } catch (FileNotFoundException e) {
  49.                 System.out.println("A file with the same name was not found");
  50.                 invalidinput = true;
  51.             } catch (IOException e) {
  52.                 System.out.println("This file cannot be opened");
  53.                 invalidinput = true;
  54.             } catch (NumberFormatException e) {
  55.                 System.out.println("Error! File contains invalid data");
  56.                 invalidinput = true;
  57.             }
  58.         } while (invalidinput);
  59.         return n;
  60.     }
  61.  
  62.     static int[] enterarr(int numb, int n) {
  63.         int[] arr = new int[n];
  64.         int temp = 0;
  65.         boolean invalidinput = false;
  66.         do {
  67.             boolean iscorrect = false;
  68.             try {
  69.                 BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
  70.                 String text = input.readLine();
  71.                 for (int i = 0; i < numb + 1; i++) {
  72.                     text = input.readLine();
  73.                 }
  74.                 String[] arra = text.split(" ");
  75.                 for (int i = 0; i < n; i++) {
  76.                     do {
  77.                         System.out.print("Enter the " + (i + 1) + " member of sequence " + (numb + 1) + " they mast not be the same ");
  78.                         arr[i] = Integer.parseInt(arra[i]);
  79.                         System.out.println(arr[i] + " ");
  80.                         iscorrect = enter(arr[i], 200000, -200000);
  81.                         for (int j = 0; j < i; j++) {
  82.                             if (arr[j] == arr[i]) {
  83.                                 temp++;
  84.                             }
  85.                         }
  86.                         if (temp > 1) {
  87.                             iscorrect = true;
  88.                             System.out.println("This is a mistake members are same! Please enter again!");
  89.                         } else {
  90.                             iscorrect = false;
  91.                         }
  92.                         temp = 0;
  93.                     } while (iscorrect);
  94.                 }
  95.                 input.close();
  96.             } catch (FileNotFoundException e) {
  97.                 System.out.println("A file with the same name was not found");
  98.                 invalidinput = true;
  99.             } catch (IOException e) {
  100.                 System.out.println("This file cannot be opened");
  101.                 invalidinput = true;
  102.             } catch (NumberFormatException e) {
  103.                 System.out.println("Error! File contains invalid data");
  104.                 invalidinput = true;
  105.             }
  106.         } while (invalidinput);
  107.         return arr;
  108.     }
  109.  
  110.     static void body() throws FileNotFoundException {
  111.         boolean iscorrect = false;
  112.         int n = numb();
  113.         int temp = 0;
  114.         int[] arr1 = new int[n];
  115.         int[] arr2 = new int[n];
  116.         arr1 = enterarr(0, n);
  117.         arr2 = enterarr(1, n);
  118.         File file = new File("D:\\output.txt");
  119.         PrintWriter prwr = new PrintWriter(file);
  120.         System.out.println("All members of sequence 2 not in sequence 1");
  121.         for (int i = 0; i < n; i++) {
  122.             for (int j = 0; j < n; j++) {
  123.                 if (arr2[i] == arr1[j]) {
  124.                     temp++;
  125.                 }
  126.             }
  127.             if (temp == 0) {
  128.                 prwr.print(arr2[i] + " ");
  129.                 System.out.print(arr2[i] + " ");
  130.             }
  131.             temp = 0;
  132.         }
  133.         prwr.close();
  134.     }
  135.  
  136.     public static void main(String[] args) throws FileNotFoundException {
  137.         System.out.println("This programm founds all members of sequence 2 not in sequence 1");
  138.         body();
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement