Home > Enterprise >  Accepting & Inputing values in a non rectangular 2D array in java
Accepting & Inputing values in a non rectangular 2D array in java

Time:07-17

I am trying to write a program to serach the index of a number in a two dimensional array. While running the code i keep getting into this exception:

Exception in thread "main" java.lang.NullPointerException: Cannot store to int array because "local4/ [local5]" is null

The code is:

import java.util.*;

public class indexSearch
{
    public static void main(String[] args) 
    {

        Scanner xyz=new Scanner(System.in);

        int no1,no2;
        System.out.println("Enter the first dimension of 2D array");
        no1=xyz.nextInt();
        int nos[][]=new int[no1][];

        for(int i =0;i<nos.length ;i  )
        {
            System.out.println("Enter the second dimension corresponding to first index " i);
            int k=xyz.nextInt();
            System.out.println("Enter the values into it now");
            for(int j=0;j<k;j--)
            {
                nos[i][k]=xyz.nextInt();
            }
        }
    
        int posi1=-1;int posi2=-1;
        System.out.println("Enter the no you want to search");
        int l=xyz.nextInt();
        for(int i=0;i<nos.length;i  )
        {
            for(int j = 0; j < nos[i].length;j  )
            {
                if(nos[i][j]==l)
                {
                    i=posi1;
                    j=posi2;
                }   
            }
        }
        if(posi1!=-1&&posi2!=-1)
        {
            System.out.println("The index was" posi1 " " posi2);
        }
        else
        {
            System.out.println("The no doesnt exist");
        }
    }    
}   
    

I have only recently started learning arrays and all the solutions for index searching a number in a 2D array were with the array being rectangular which has lead me to think that there is no way to input from user values for 2D non rectangular array whose size is also to be taken from user.

CodePudding user response:

There are several issues in the code.

  1. nos[i] is null. So you may initialize it as nos[i] = new int[k]; when you get the value of k
  2. you have j-- in the for loop with j starting with 0. have to be j .
  3. You're inserting to nos[i][k] which cause ArrayIndexOutOfBounds exception. Should be nos[i][j]=xyz.nextInt().

Fixed algo is given below.

import java.util.*;

public class indexSearch
{
    public static void main(String[] args) 
    {

        Scanner xyz=new Scanner(System.in);

        int no1,no2;
        System.out.println("Enter the first dimension of 2D array");
        no1=xyz.nextInt();
        int nos[][]=new int[no1][];

        for(int i =0;i<nos.length ;i  )
        {
            System.out.println("Enter the second dimension corresponding to first index " i);
            int k=xyz.nextInt();
            nos[i] = new int[k];
            System.out.println("Enter the values into it now");
            for(int j=0;j<k;j  )
            {
                nos[i][j]=xyz.nextInt();
            }
        }
    
        int posi1=-1;int posi2=-1;
        System.out.println("Enter the no you want to search");
        int l=xyz.nextInt();
        for(int i=0;i<nos.length;i  )
        {
            for(int j = 0; j < nos[i].length;j  )
            {
                if(nos[i][j]==l)
                {
                    i=posi1;
                    j=posi2;
                }   
            }
        }
        if(posi1!=-1&&posi2!=-1)
        {
            System.out.println("The index was" posi1 " " posi2);
        }
        else
        {
            System.out.println("The no doesnt exist");
        }
    }    
}

CodePudding user response:

Currently nos[i] is null when you try to insert values, before taking the values, initialize it as nos[i] = new int[k];.

Unrelated to your bug, I would recommend giving variables more descriptive names, such as scanner instead of xyz, since this makes it a lot easier for other developers to understand your code.

CodePudding user response:

There are several errors in your code. Firstly, assign assign some value to nos[i] and change nos[i][k]=xyz.nextInt(); to nos[i][j]=xyz.nextInt(); in this code block

for(int i =0;i<nos.length ;i  )
{
            System.out.println("Enter the second dimension corresponding to first index " i);
            int k=xyz.nextInt();
            nos[i] = new int[k];
            System.out.println("Enter the values into it now");
            for(int j=0;j<k;j  )
            {
                nos[i][j]=xyz.nextInt();
            }
            //....

Secondly, change j-- to j here:

 System.out.println("Enter the values into it now");
            for(int j=0;j<k;j  )
            {
                nos[i][j]=xyz.nextInt();
            }

Thirdly, change from this:

i=posi1;
j=posi2;

To this:

posi1=i;
posi2=j;

Full working code here:

import java.util.*;

public class indexSearch
{
    public static void main(String[] args)
    {

        Scanner xyz=new Scanner(System.in);

        int no1,no2;
        System.out.println("Enter the first dimension of 2D array");
        no1=xyz.nextInt();
        int nos[][]=new int[no1][];

        for(int i =0;i<nos.length ;i  )
        {
            System.out.println("Enter the second dimension corresponding to first index " i);
            int k=xyz.nextInt();
            nos[i] = new int[k];
            System.out.println("Enter the values into it now");
            for(int j=0;j<k;j  )
            {
                nos[i][j]=xyz.nextInt();
            }
        }

        int posi1=-1;int posi2=-1;
        System.out.println("Enter the no you want to search");
        int l=xyz.nextInt();
        for(int i=0;i<nos.length;i  )
        {
            for(int j = 0; j < nos[i].length;j  )
            {
                if(nos[i][j]==l)
                {
                    posi1=i;
                    posi2=j;
                }
            }
        }
        if(posi1!=-1&&posi2!=-1)
        {
            System.out.println("The index was" posi1 " " posi2);
        }
        else
        {
            System.out.println("The no doesnt exist");
        }
    }
}

Not related to this problem, but please try to give meaningful names to the variables and fix the indentation of your code so that it will be easier for other developers to understand your code and help you out.

Happy coding.

  • Related