S
S
Sawayadi2020-11-26 13:56:56
Java
Sawayadi, 2020-11-26 13:56:56

How to enter multiple lines and execute for each method?

I have a code that performs a specific operation on a number string. I need to make it so that several lines are entered and for each of them this code is executed separately. Here is the code for one row:

package com.company; 
import java.util.*;
public class Main {
 
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
HashSet<String> test = new HashSet<String>();
    System.out.println("\nВведите строку: ");
    String[] inputArr = in.nextLine().split(" ");
 
    for (int i=0; i<inputArr.length; i++) {
        if (Integer.parseInt(inputArr[i])>9) {
            for (int j=i+1; j<inputArr.length; j++) {
                if (!inputArr[i].equals(inputArr[j]) && inputArr[i].length() == inputArr[j].length()) {
                    String[] temp1 = inputArr[i].split("");
                    String[] temp2 = inputArr[j].split("");
                    boolean check1 = Arrays.asList(temp1).containsAll(Arrays.asList(temp2));
                    boolean check2 = Arrays.asList(temp2).containsAll(Arrays.asList(temp1));
                    if (check1 && check2) {
                        test.add(inputArr[i]+'-'+inputArr[j]);
                    }
                }
            }
        }
    }
    System.out.println("Одинаковые числа: " + test);
}
}

I tried to do it myself, but an error is displayed when I enter a number for the number of lines (for any numbers it displays an error).
package com.company; 
import java.util.*;
public class Main {
 
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
System.out.println("Введите количество рядков: ");
    int n= in.nextInt();
    String[] strings = new String[n];
HashSet<String> test = new HashSet<String>();
    System.out.println("\nВведите рядок: ");
    String[] inputArr = in.nextLine().split(" ");
 
    for (int i=0; i<inputArr.length; i++) {
        if (Integer.parseInt(inputArr[i])>9) {
            for (int j=i+1; j<inputArr.length; j++) {
                if (!inputArr[i].equals(inputArr[j]) && inputArr[i].length() == inputArr[j].length()) {
                    String[] temp1 = inputArr[i].split("");
                    String[] temp2 = inputArr[j].split("");
                    boolean check1 = Arrays.asList(temp1).containsAll(Arrays.asList(temp2));
                    boolean check2 = Arrays.asList(temp2).containsAll(Arrays.asList(temp1));
                    if (check1 && check2) {
                        test.add(inputArr[i]+'-'+inputArr[j]);
                    }
                }
            }
        }
    }
    System.out.println("Одинаковые числа: " + test);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sand, 2020-11-26
@Sawayadi

import java.util.*;
public class Main {
public static int n = 0;
public static void main(String[] args) {
    int n = 3;
    HashSet<String> test;
    String[] inputArr;
    Scanner in = new Scanner(System.in);
    while (n > 0) {
    System.out.println("\nВведите строку: ");
    inputArr = in.nextLine().split(" ");
    test = new HashSet<String>();
    for (int i=0; i<inputArr.length; i++) {
        if (Integer.parseInt(inputArr[i])>9) {
            for (int j=i+1; j<inputArr.length; j++) {
                if (!inputArr[i].equals(inputArr[j]) && inputArr[i].length() == inputArr[j].length()) {
                    String[] temp1 = inputArr[i].split("");
                    String[] temp2 = inputArr[j].split("");
                    boolean check1 = Arrays.asList(temp1).containsAll(Arrays.asList(temp2));
                    boolean check2 = Arrays.asList(temp2).containsAll(Arrays.asList(temp1));
                    if (check1 && check2) {
                        test.add(inputArr[i]+'-'+inputArr[j]);
                    }
                }
            }
        }
    }
    System.out.println("Одинаковые числа: " + test);
    n -= 1;
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question