How to create a list in Java by taking integers separated by spaces? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

8th Aug 2022, 3:49 PM
syed fahad
syed fahad - avatar
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(); } } }
21st Apr 2023, 7:05 PM
Aryan Karumuri
Aryan Karumuri - avatar