-1

The problem requires me to: test the given array(s) and see if there's number 5 in it. If there is a 5, return False. Otherwise, return true. I got it to work... well, I know it's not in an efficient way. The only problem is that in case 1 and 6 I got:Error java.lang.ArrayIndexOutOfBoundsException, and I don't even know why. The rest works just fine.

import java.util.*;

public class testCases
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("Which case do you wish to test? ");

        int testCase = kb.nextInt();

        switch(testCase)
        {
            case 1:
                    System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                    break;

            case 2:
                    System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5));
                    break;

            case 3:
                    System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4));
                    break;

            case 4:
                    System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4));
                    break;
            case 5:
                    System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18));
                    break;

            case 6:
                    System.out.println("noFives(0) = " + noFives(0));
                    break;

            case 7:
                    System.out.println("noFives(5) = " + noFives(5));
                    break;

            default:
                    System.out.println("noFives() = " + noFives());

        }

    }

    public static boolean noFives(int ... n)
    {

        if(n.length == 0)
        return true;

        else if(n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5)
        return false;

        else
        return true;

    }

}
antzshrek
  • 9,276
  • 5
  • 26
  • 43
Apples
  • 1
  • 4

3 Answers3

0

A few factors play into the inconsistent behavior of your implementation:

  1. The || operator short-circuits. I.e., in noFives() if n[0] == 5, it doesn't test any of the other terms.

  2. In java, accessing an element beyond the end of an array causes the OutOfBounds exception.

so you get the exception when the chain of || operations doesn't short-circuit before accessing an index outside of the array. Cases 1 and 6 have no fives to cause this short-circuit.

lockcmpxchg8b
  • 2,205
  • 10
  • 16
0

You should use a loop to handle changing lengths of n. Rewriting your code

import java.util.*;

public class testCases
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("Which case do you wish to test? ");

        int testCase = kb.nextInt();

        switch(testCase)
        {
            case 1:
                    System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                    break;

            case 2:
                    System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5));
                    break;

            case 3:
                    System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4));
                    break;

            case 4:
                    System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4));
                    break;
            case 5:
                    System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18));
                    break;

            case 6:
                    System.out.println("noFives(0) = " + noFives(0));
                    break;

            case 7:
                    System.out.println("noFives(5) = " + noFives(5));
                    break;

            default:
                    System.out.println("noFives() = " + noFives());

        }

    }

    public static boolean noFives(int ... n)
    {

        if(n.length == 0)
        return true;

        else {
            for(int i=0; i < n.length; i++){
                if(n[i] == 5)
                    return false;
            }
        }

        return true;

    }

}
abskmj
  • 760
  • 3
  • 6
0

This is due to the parameters being sent to the method noFives(int.. n) for case statement 1 you are passing 5 parameters

case 1:
                System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                break;

whereas in noFives(int... n) method you are checking for index more than 5 hence ArrayIndexOutOfBoundsException

 else if (n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5)

i would suggest you to change the method noFives(int ..n) like:

public static boolean noFives(int... n) {

        if (n.length == 0)
            return true;

        for (int i = 0; i < n.length ; i++) {
            if (n[i] == 5){
                return false;
            }
        }
        return true;
    }
Ravik
  • 694
  • 7
  • 12