0

Hello please help me ?? When i run this code in solo learn app the input option is coming but how to take array input ..

import java.util.Scanner; class Dcoder { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<=n;i++) arr[i]=sc.nextInt(); for(int k=0;k<=n;k++) System.out.println(arr[k]); } }

7th Aug 2018, 6:22 AM
Md Raashid
Md Raashid - avatar
8 Respuestas
+ 1
I don't have WA, besides, sharing personal information is not allowed here, it is against privacy policy. I ran that code in Code Playground, and with the for loops condition fixed, it runs OK, you can post the error message here btw ...
7th Aug 2018, 9:52 AM
Ipang
+ 2
import java.util.Scanner; class Dcoder { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; // previously: for(int i=0;i<=n;i++) for(int i=0;i<n;i++) arr[i]=sc.nextInt(); // previously: for(int k=0;k<=n;k++) for(int k=0;k<n;k++) System.out.println(arr[k]); } } Please read the comments over the for loops, you only needed to change the <= into < as I had shown you in my first response.
7th Aug 2018, 7:21 AM
Ipang
+ 1
Enter the numbers in different lines, separated by new line (Enter), example: 100 200 300 400 500 P.S. Your loops need corrections, an array of <n> elements are indexed from 0 ~ <n> -1. If you do "i <= n" you will get array subscription error, because it tries to reference arr[<n>] which is not allocated/accessible. for(int i = 0; i < n ; i++) for(int k = 0; k < n ; k++) Hth, cmiiw
7th Aug 2018, 6:44 AM
Ipang
+ 1
some error is coming after separating in new line
7th Aug 2018, 6:50 AM
Md Raashid
Md Raashid - avatar
0
please run in solo learn app
7th Aug 2018, 6:49 AM
Md Raashid
Md Raashid - avatar
0
Yes, the error is triggered by the fault in the loops, as I had shown you above, change the for loops as I suggested and you'll have your code running just fine : )
7th Aug 2018, 7:03 AM
Ipang
0
please send me the complete code you have run successfully
7th Aug 2018, 7:09 AM
Md Raashid
Md Raashid - avatar
0
send me your whatsapp number ... what type of error is coming I will send you
7th Aug 2018, 9:48 AM
Md Raashid
Md Raashid - avatar