So I wrote code that does not enter values ​​into a column, but into a string:

Scanner sc2 = new Scanner(System.in); String scString2 = sc.nextLine(); String[] scStrings2 = scString2.split(" "); int[] scNumbers2 = new int[powyk1]; for(int i = 0; i < powyk1; i++) { if (Integer.valueOf(scStrings2[i])>0 && Integer.valueOf(scStrings2[i])<Math.pow(10,9)+7); scNumbers2[i] = Integer.valueOf(scStrings2[i]); } 

The data in the array are entered as follows:

 2 2 3 4 5 6 

And how to make sure that the data are entered in pairs:

 2 2 4 5 6 7 
  • ; - remove firstly this after if - Anton Sorokin
  • @ Anton238 removed - CR7
  • You can do this: the user makes the input - two-space-two - you write it to the string variable with pom. nextLine, then transfer to the array, delete the middle element (you can assign the first and third e-s to another array), and so on three times. - Anton Sorokin
  • Use nextint() for all. It will be much easier - Oleksiy Morenets
  • @ OleksiyMorenets can throw an example - CR7

1 answer 1

 public static void main(String[] args) { int powyk1 = 6; // я задал вручную Scanner sc = new Scanner(System.in); // String scString2 = sc.nextLine(); // String[] scStrings2 = scString2.split(" "); int[] scNumbers2 = new int[powyk1]; for (int i = 0; i < powyk1; i++) { int num = sc.nextInt(); if (num > 0 && num < Math.pow(10, 9) + 7) scNumbers2[i] = num; } System.out.println(Arrays.toString(scNumbers2)); } 
  • No, you do not understand, I have to enter two digits in different lines up to the exact value - CR7
  • i.e., when we meet the value <= 0 or >= Math.pow(10, 9) + 7 , do you need to complete the input? Then add an else break; - Oleksiy Morenets