0
How to create a list in Java by taking integers separated by spaces?
I used to create a list by taking Integers separated by spaces in Python using list(map(int,input().split())) . I want to know is there any similar way in Java to accept input without using For loop?
1 Answer
+ 1
I think there might be some couple of ways. But I got one idea, that is you can use scanner class for taking the integers separated by spaces.
Ex:
import java.util.*;
class A {
public static void main(String[] args)
{
int[] nums = new int[5];
System.out.println("Enter input ");
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
}
}