How do I fill an array with elements from the keyboard?



An array is given. Determine the number of maximum elements in the array

The first line specifies the number of array elements (no more than 100).)

In the second line, the array elements are entered.


I know how to find the maximum value in an array with random numbers, but how can I push keyboard elements into the array (as in the problem) if they are written in a string separated by a space? And then how to work with them?

Author: LEQADA, 2015-10-16

4 answers

I will answer the question "How to fill an array with elements entered from the keyboard". To do this, we need Scanner.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); // Объявляем Scanner
    System.out.println("Enter array length: ");
    int size = input.nextInt(); // Читаем с клавиатуры размер массива и записываем в size
    int array[] = new int[size]; // Создаём массив int размером в size
    System.out.println("Insert array elements:");
    /*Пройдёмся по всему массиву, заполняя его*/
    for (int i = 0; i < size; i++) {
        array[i] = input.nextInt(); // Заполняем массив элементами, введёнными с клавиатуры
    }
    System.out.print ("Inserted array elements:");
    for (int i = 0; i < size; i++) {
        System.out.print (" " + array[i]); // Выводим на экран, полученный массив
    }
    System.out.println();
}

And then work with the array in the same way as you used to work with an array filled with random numbers.

 9
Author: LEQADA, 2015-10-16 17:40:02

But for ArrayList, reworked a little, try adding with NameofList.add(something)

public static void main(String[] args) {

        Scanner input = new Scanner(System.in); // Объявляем Scanner
        System.out.println("Enter array length: ");
        int size = input.nextInt(); // Читаем с клавиатуры размер массива и записываем в size

        List<Integer> SubArray2 = new ArrayList<>(); // Создаём массив int размером в size

         System.out.println("Insert array elements:");
        /*Пройдёмся по всему массиву, заполняя его*/

        for (int i = 0; i < size; i++) {
            SubArray2.add(input.nextInt()); // Заполняем массив элементами, введёнными с клавиатуры
        }

        System.out.print ("Inserted array elements:");
        System.out.println(SubArray2);
        }
 1
Author: Arsen, 2019-12-26 17:22:12

So, like, prettier!?

public static void main(String[] agrs)
{
    int[] a = new int[7];
    int i = 0;
    Scanner in = new Scanner(System.in);


    for (int element : a)
    {       

        System.out.print("Введите " + (i + 1) + "-й элемент массива: ");
        a[i] = in.nextInt();

        System.out.println(a[i] + " - ");

        i++;
    }

    for (int element : a)

        System.out.print(element + " ");


}

Or so:

public static void main(String[] agrs)
{
    int[] a = new int[7];
    int i = 0;
    Scanner in = new Scanner(System.in);

    for (int element : a)
    {       

        System.out.print("Введите " + (i + 1) + "-й элемент массива: ");
        a[i] = in.nextInt();

        System.out.println(a[i] + " - ");

        i++;
    }

    System.out.println(Arrays.toString(a));
}
 0
Author: Иггр, 2016-03-17 19:27:20
import java.util.Scanner;
public class PerBor_Mas {

    public static void main(String[] args) throws InterruptedException {
        int n = vvod.nextInt();
        int[] A = new int[n];
        int k = vvod.nextInt();

        Scanner vvod = new Scanner(System.in);
        System.out.println("Введите длину одномерного массива");


        System.out.println("Введите первый элемент массива");

        System.out.println("Водится массив");

        for (int i = 0; i < n; i++) {
            System.out.print(A[i] + k + "\t");
            k++;
        }
        System.out.println(" ");

    }
}
 0
Author: TaraBar Barb, 2018-02-25 15:28:44