Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import sun.awt.OSInfo;
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.FileNotFoundException;
- import java.io.File;
- import java.io.PrintWriter;
- public class Main {
- static boolean enter(int temp, int max, int min) {
- Scanner scanner = new Scanner(System.in);
- boolean iscorrect = true;
- do {
- try {
- if ((temp > min - 1) && (temp < max + 1)) {
- iscorrect = false;
- } else {
- System.out.println("This is a mistake. Please enter again!");
- }
- } catch (Exception e) {
- scanner.nextLine();
- System.out.println("This is a mistake. Please enter again!");
- iscorrect = true;
- }
- }
- while (iscorrect);
- return iscorrect;
- }
- static int numb() {
- boolean invalidinput = false;
- int n = 0;
- do {
- boolean iscorrect = false;
- try {
- BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
- String text = input.readLine();
- do {
- System.out.print("Enter the number of sequence members ");
- n = Integer.parseInt(text);
- System.out.println(n);
- iscorrect = enter(n, 10, 1);
- } while (iscorrect);
- } catch (FileNotFoundException e) {
- System.out.println("A file with the same name was not found");
- invalidinput = true;
- } catch (IOException e) {
- System.out.println("This file cannot be opened");
- invalidinput = true;
- } catch (NumberFormatException e) {
- System.out.println("Error! File contains invalid data");
- invalidinput = true;
- }
- } while (invalidinput);
- return n;
- }
- static int[] enterarr(int numb, int n) {
- int[] arr = new int[n];
- int temp = 0;
- boolean invalidinput = false;
- do {
- boolean iscorrect = false;
- try {
- BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
- String text = input.readLine();
- for (int i = 0; i < numb + 1; i++) {
- text = input.readLine();
- }
- String[] arra = text.split(" ");
- for (int i = 0; i < n; i++) {
- do {
- System.out.print("Enter the " + (i + 1) + " member of sequence " + (numb + 1) + " they mast not be the same ");
- arr[i] = Integer.parseInt(arra[i]);
- System.out.println(arr[i] + " ");
- iscorrect = enter(arr[i], 200000, -200000);
- for (int j = 0; j < i; j++) {
- if (arr[j] == arr[i]) {
- temp++;
- }
- }
- if (temp > 1) {
- iscorrect = true;
- System.out.println("This is a mistake members are same! Please enter again!");
- } else {
- iscorrect = false;
- }
- temp = 0;
- } while (iscorrect);
- }
- input.close();
- } catch (FileNotFoundException e) {
- System.out.println("A file with the same name was not found");
- invalidinput = true;
- } catch (IOException e) {
- System.out.println("This file cannot be opened");
- invalidinput = true;
- } catch (NumberFormatException e) {
- System.out.println("Error! File contains invalid data");
- invalidinput = true;
- }
- } while (invalidinput);
- return arr;
- }
- static void body() throws FileNotFoundException {
- boolean iscorrect = false;
- int n = numb();
- int temp = 0;
- int[] arr1 = new int[n];
- int[] arr2 = new int[n];
- arr1 = enterarr(0, n);
- arr2 = enterarr(1, n);
- File file = new File("D:\\output.txt");
- PrintWriter prwr = new PrintWriter(file);
- System.out.println("All members of sequence 2 not in sequence 1");
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (arr2[i] == arr1[j]) {
- temp++;
- }
- }
- if (temp == 0) {
- prwr.print(arr2[i] + " ");
- System.out.print(arr2[i] + " ");
- }
- temp = 0;
- }
- prwr.close();
- }
- public static void main(String[] args) throws FileNotFoundException {
- System.out.println("This programm founds all members of sequence 2 not in sequence 1");
- body();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement