0

What is the problem in my code?

import java.util.*; class program { public static void main(String args[]) { int num; int c=1; int gap; int n; int[] arr = new int[n]; Scanner sc = new Scanner(System.in); System.out.println("Enter array size"); n = sc.nextInt(); for(i = 0; i < arr.length;i++) { System.out.println("Enter" + c + "number"); num=sc.nextInt(); arr[i] = num; c++; } gap = arr[arr.length-1] - arr[0]; System.out.println("The longest gap is" + gap); } }

3rd Jan 2019, 6:03 AM
Prince Gupta
Prince Gupta - avatar
1 Answer
+ 1
I ran the code the first error rose at the array,Create the scanner object at the start of the program then get 'n' from the user(error solved)..The next error happened at the first for loop,Add the type 'int' to i in the loop,"i" alone wont work without a type...
3rd Jan 2019, 6:11 AM
Mensch
Mensch - avatar