error of using scanner for assing lenght of array and numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

error of using scanner for assing lenght of array and numbers

import java.util.Scanner; public class Solution { public static void main (String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] numbers = new int [N]; System.out.println(N); for (int i=0; i < N; i++) { numbers[i] = in.nextInt(); } } } Output in SoloLearn Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Solution.main(Solution.java:11)

3rd Jan 2018, 10:17 PM
Tsarov
1 Answer
+ 2
In sololearn, you need to write all the input values before compiling starts. You're only giving the size, and program isn't going to stop n popup input-field for arrays input. If you're giving size 5, then give the 5 values too. For eg, 5 1 2 3 4 5 1st num is the size, the next 5 are values for array!
3rd Jan 2018, 11:27 PM
Harjeet Singh
Harjeet Singh - avatar