0

Input Format:

The first line has an integer . In each of the next lines there will be an integer denoting number of integers on that line and then there will be space-separated integers. In the next line there will be an integer denoting number of queries. Each query will consist of two integers x and y.

Output Format:

In each line, output the number located in yth position of xth line. If there is no such position, just print "ERROR!"

   public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    Integer arr[][] = new Integer[x][];
    arr[x][100] = null; 
     for(int i=0;i < x;i++){
            int y = in.nextInt(); 
         for(int j = 0;j<y;j++){
             arr[i][j] = in.nextInt();
         }       
        }
    int z = in.nextInt();
     for(int k=0;k<z;k++){
         int p = in.nextInt();
         int q = in.nextInt();   
         if(arr[p-1][q-1] ==  null){

             System.out.printf("%s\n","ERROR!");

         }
          else 
              System.out.printf("%d\n",arr[p-1][q-1]); 

     }

}

When I run this I'm getting error as

 Solution.java:23: error: incomparable types: int and <null>
           if(arr[p-1][q-1] ==  null){

How do I avoid this error in future?

3
  • That depends on what you are trying to do but you can use an Integer array instead of an int array and initialize it's elements with null. Commented May 1, 2018 at 11:14
  • can you post more of your code? Commented May 1, 2018 at 11:23
  • Seems like a Problem from hackerrank. Please be more specific what you want to ask? You can also check if your code throws ArrayIndexOutOfBoundException for location which doesn't exist Commented May 1, 2018 at 12:02

2 Answers 2

1

I can't reproduce your problem. When I compile your code, I get an error that is solved by adding an import statement. Then when the code is runnable, I get an ArrayIndexOutOfBoundsException.

But my guess is that your code was originally declaring arr like this:

int arr[][] = new int[x][];

Then this

if (arr[p-1][q-1] == null) {

will give you that the compilation error that you reported.

The error message is telling you that you cannot compare arr[p-1][q-1] with null, because arr[p-1][q-1] evaluated to an int, and a primitive type (such an int) is not comparable with null.

Why? Because null is not a valid integer value.


How to check an element in array exists or not in java?

It depends.

For an array whose base type is a primitive type, there is no way to do it. Every element in (for example) an int[] exists, and has a value that is an integer. There is simply no way for an element to "not exist" ... provided that the index for the element is in the range 0 to array.length - 1.

For an array whose base type is a reference type (e.g. Integer), an element can have the value null, and you could use that to mean that no value exists. And if you do that an == null test is valid.

Sign up to request clarification or add additional context in comments.

Comments

0
public static void main(String args[]) throws ArrayIndexOutOfBoundsException
{
   //Your code..
    int p = in.nextInt();
    int q = in.nextInt();
    try
    {
        int r = arr[p-1][q-1];
        System.out.printf("%d\n",arr[p-1][q-1]);
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        System.out.printf("%s\n","ERROR!");   
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.