+ 5

what is wrong in the codes i would appreciate any help

https://code.sololearn.com/cS4Ek65nd2Up/?ref=app

11th Apr 2017, 4:18 PM
shobhit
shobhit - avatar
8 ответов
+ 8
You need to create an array, first in main method. int[] x={5,6,7,9,0}; Then, System.out.println(hello(x));
11th Apr 2017, 4:23 PM
Meharban Singh
Meharban Singh - avatar
+ 8
5 is not an array. Method hello requires an array.
11th Apr 2017, 4:20 PM
Meharban Singh
Meharban Singh - avatar
+ 7
Also,you need to declare hello as static. public static ..........hello .....
11th Apr 2017, 4:25 PM
Meharban Singh
Meharban Singh - avatar
+ 7
There's a couple problems here: 5 is not an array. You would need something like this: int[] myArray = new int[] {5}; System.out.print(hello(myArray)); You're calling a non-static method from a static method, this isn't allowed since there's no instance of the class to call the non-static method from. so change: public int hello(... etc) to: private static int hello(...etc.) Then that's it. Note* the reason I made it private instead of public is simply because there's no need to have it public (it's considered good programming practice / standard).
11th Apr 2017, 4:25 PM
Rrestoring faith
Rrestoring faith - avatar
+ 7
@Merharban @Edward int[] a = {5}; is the same as int[] a = new int[]{5}; The difference is that on 'new' declaration by giving the array it's size, it will fill the default values in for you. so: int[] a = new int[5]; is: [0,0,0,0,0]. rather than having to write: int[] a = {0,0,0,0,0}. which is: [0,0,0,0,0]. So, by writing int[] a = {}; your forced to put in all the values. In this case, that could be better though. That method also cannot be used when the array is already declared. Example: int[] a; a = {5}; // is illegal
11th Apr 2017, 4:42 PM
Rrestoring faith
Rrestoring faith - avatar
+ 5
@Restoring faith,I don't think you can create an array like that. It should be int[] myArray=new int[5];
11th Apr 2017, 4:28 PM
Meharban Singh
Meharban Singh - avatar
+ 5
you can create an array like int[ ] arr = {5}; its simpler than @restoring faiths method
11th Apr 2017, 4:31 PM
Edward
+ 4
can u plz explain how to do it in parameters.
11th Apr 2017, 4:21 PM
shobhit
shobhit - avatar