0

Write a program that return the first 3 numbers greater than 11. If there are fewer than 3 numbers greater than 11, then return as many as you find. You will always return an array size 3. Zeroes will fill the array if there are not 3 numbers greater than 11. This is the code that i write but it giving unexpected answer. Can anyone correct this code.

public class RayGetNums
{
    //method go will return an array
    //containing the first 3 numbers
    //greater than 11
public static int[] go(int[] ray)
{
    int[] res = new int[]{0,0,0};
    int count = 0;
    for (int i = 0; i < ray.length && count < 3; i++) {
        if ( ray[i] > 11 ) {
            res[count++] = ray[i];
        }
    }
    return res;
}
}

The Runner that i use with this question is this:

class Runner
 {
  public static void main(String[] args)
  {
    RayGetNums rt = new RayGetNums();

        System.out.println( rt.go( new int[]{-99,1,2,3,4,5,6,7,8,9,10,12345} ) );
        System.out.println( rt.go( new int[]{10,9,8,7,6,5,4,3,2,1,-99} ) );
        System.out.println( rt.go( new int[]{10,20,30,40,50,-11818,40,30,20,10} ) );
        System.out.println( rt.go( new int[]{32767} ) );
        System.out.println( rt.go( new int[]{255,255} ) );
        System.out.println( rt.go( new int[]{9,10,-88,100,-555,1000} ) );
        System.out.println( rt.go( new int[]{10,10,10,11,456} ) );
        System.out.println( rt.go( new int[]{-111,1,2,3,9,11,20,30} ) );
        System.out.println( rt.go( new int[]{9,8,7,6,5,4,3,2,0,-2,-989} ) );
        System.out.println( rt.go( new int[]{12,15,18,21,23,1000} ) );
        System.out.println( rt.go( new int[]{250,19,17,15,13,11,10,9,6,3,2,1,-455} ) ); 
        System.out.println( rt.go( new int[]{9,10,-8,10000,-5000,1000} ) );
  }
}

The answers that i get with this code is:

[I@4617c264
[I@36baf30c
[I@7a81197d
[I@5ca881b5
[I@24d46ca6
[I@4517d9a3
[I@372f7a8d
[I@2f92e0f4
[I@28a418fc
[I@5305068a
[I@1f32e575
[I@279f2327

The answers that i need we this runner:

[12345, 0, 0]
[0, 0, 0]
[20, 30, 40]
[32767, 0, 0]
[255, 255, 0]
[100, 1000, 0]
[456, 0, 0]
[20, 30, 0]
[0, 0, 0]
[12, 15, 18]
[250, 19, 17]
[10000, 1000, 0]

Can anyone correct the error so i have correct answers. Thank you

Faheem
  • 42
  • 9
  • You're seeing the `toString()` output of a standard Java int array. You need to print the items yourself as per [this question](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). Simply printing out the String returned from `java.util.Arrays.toString(yourArray)` would work. – Hovercraft Full Of Eels Jan 13 '20 at 01:03
  • @HovercraftFullOfEels It give error and it says: (method Object.toString() is not applicable) – Faheem Jan 13 '20 at 01:14
  • Then you're doing it wrong, but you've not shown how you're using it and so we can't help – Hovercraft Full Of Eels Jan 13 '20 at 01:18
  • @HovercraftFullOfEels I am using in runner like this – Faheem Jan 13 '20 at 01:22
  • public static void main(String[] args) { RayGetNums rt = new RayGetNums(); System.out.println( rt.go( java.util.Arrays.toString(-99,1,2,3,4,5,6,7,8,9,10,12345 )) ); – Faheem Jan 13 '20 at 01:23
  • Lord no. You need to enclose the `Arrays.toString(...)` around your int array that is returned by the method. – Hovercraft Full Of Eels Jan 13 '20 at 01:24
  • `System.out.println(java.util.Arrays.toString(rt.go(-99,1,2,3,4,5,6,7,8,9,10,12345 )));` Check out the duplicate used to close this question – Hovercraft Full Of Eels Jan 13 '20 at 01:25

0 Answers0