Home > Software engineering >  How to validate 2D array values in Java
How to validate 2D array values in Java

Time:10-03

I am new to Java and having difficulty performing input validation on a 2D array of integers. I am trying to check to determine if the array has had any negative integers added and if so to then throw an exception.

What I have tried is:

int [][] myArray = {
        {2,-2,2,2},
        {3,3,3,3}};

for(int i = 0; i < myArray.length; i  ){
            for(int j = 0; j < myArray[i].length; j  ){
                 if(myArray[1] < 0 ){
                     throw new IllegalArgumentException("Negative integer.");
            }
        }

However, as many will probably gather from looking at my code, I get the error

Incompatible types: int[] cannot be converted to int

Is there a way to perform input validation in some way to check for non-positive integers? I have so far not been able to come up with a good solution or work around.

CodePudding user response:

You are very close

  • Related