+ 1

My code is not working as I want it to

I'm beginning to learn java and have written the following code to find the sum of the elements present in an array using what I've learnt so far... import java.util.Scanner; public class myClass { public static void main(String h[]) { Scanner s=new Scanner(System.in); int[] arr=new int[100]; System.out.print("Enter the size of the array : "); int n=s.nextInt(); for(int i=0;i<n;i++) System.out.println("Enter the "+(i+1)+" Element of arr : "+(arr[i]=s.nextInt())); //this line s.close(); int sum=0; for(int t:arr) sum+=t; System.out.println("Sum of all the elements of arr is : "+sum); } } The line I use to get the Input from the user works like so: Enter the size of the array : 5 1 Enter the 1 Element of arr : 1 2 Enter the 2 Element of arr : 2 3 Enter the 3 Element of arr : 3 4 Enter the 4 Element of arr : 4 5 Enter the 5 Element of arr : 5 Sum of all the elements of arr is : 15 I want it to Print "Enter the n element of arr : " then get the input from the user, not get the input from the user first then print what the user has already inputted. Yes, I know the scanner function can be placed after the print function to get the desired output, but I want to know if what I'm asking for is possible. Thanks in advance!

6th Jan 2017, 7:44 AM
Deepak Balaji
Deepak Balaji - avatar
5 Answers
+ 8
That's just a quirk with the online compiler. All inputs must be sent at once, along with your code, and you get sent back the result with it. Those inputs are being taken by your program when they are prompted for, they're just not given in realtime. If you ran this through a compiler on a computer and ran it, it would work "correctly".
6th Jan 2017, 8:46 AM
Tamra
Tamra - avatar
+ 4
I know this is unrelated to the issue, but why do you request for the size of the array after defining the size for it? Wouldn't it make more sense if you made the array as big as requested? This way, the array size is too big, and if someone requested for an array bigger than 100, the program would break.
6th Jan 2017, 9:04 AM
Dao
Dao - avatar
+ 2
@Tamra I tried running this on Eclipse (4.6.2) on my laptop and it does the same thing. I even tried running it through the command prompt, But same result... Maybe I'm missing something? @Ken Kaneki Thank you for pointing that out! int[] arr=new int[n]; is definitely better than what I had coded :) It's just that in C++ you need to assign the array size as a constant... and I just assumed that was the case here as well.
6th Jan 2017, 9:16 AM
Deepak Balaji
Deepak Balaji - avatar
+ 2
Yes, as you wrote. To get the right result as you wish you must placed the scanner function after the print function. And get both into {block} for(int i=0;i<n;i++) {System.out.println("Enter the "+(i+1)+ " Element of arr : "); arr[i]=s.nextInt(); } But there can be another probably fault. You must know, this way you do not enter the size of the array, but only a number of input values into first n arrays items. The size of array stays the same, 100 items. So - the end loop for(int t:arr) sum+=t; counts all 100 items (the next from n are zero), So, teh right way is }for example, for(int i=0;i<n;i++) sum+=arr[i];
6th Jan 2017, 10:29 AM
Petr Hatina
Petr Hatina - avatar
+ 1
@Petr Hatina Thank you for that clarification. Also, the statement int[] arr=new int[100]; was an error in my part 😅. I have now changed it to int[] arr=new int[n]; so that the for each loop won't run unnecessarily.
6th Jan 2017, 10:37 AM
Deepak Balaji
Deepak Balaji - avatar