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]); } }
8 Antworten
+ 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 ...
+ 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.
+ 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
+ 1
some error is coming after separating in new line
0
please run in solo learn app
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 : )
0
please send me the complete code you have run successfully
0
send me your whatsapp number ... what type of error is coming I will send you