Read integers separated with whitespace into int[] array

前端 未结 10 1888
陌清茗
陌清茗 2020-12-13 19:13

I read line with

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();

Example input is

10条回答
  •  無奈伤痛
    2020-12-13 19:24

    Simple answer would be read integer inside the for loop using nextInt() method of scanner class.

    Here is the implementation :

    import java.util.Scanner;
    
    public class DriverMain {
        public static void main(String args[]){
            Scanner scanner = new Scanner(System.in);
            
            System.out.println("Enter the size of an array: ");
            int size = scanner.nextInt();
    
            System.out.println("Enter array elements separated by single space.");
            int[] numbers = new int[size];
            for(int i=0;i

提交回复
热议问题