0

So I want to skip the first and last elements of the array to initialize. What am I doing wrong?

public static void main(String args[]) throws Exception {

    //Write code here
    Scanner sc = new Scanner(System.in);
    System.out.println("Input Rows: ");
    int m = sc.nextInt();
    System.out.println("Input Columns: ");
    int n = sc.nextInt();
    System.out.println("Enter values: ");
    int[][] arr = new int[m][n];

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (arr[i][j] == arr[0][0] || arr[i][j] == arr[m][n]) {
                continue;
            } else {
                arr[i][j] = sc.nextInt();
            }
        }

        System.out.println();
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }

}

Here is my output:

Input Rows: 
3

Input Columns:
3

Entered Values:

0 0 0 

0 0 0 

0 0 0 
0

4 Answers 4

2

You need to change the if condition inside the loop like following:

for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
        if ((i == 0 && j==0) || (i == m -1 && j == n -1)) {
            continue;
        } else {
            arr[i][j] = sc.nextInt();
        }
    }

    System.out.println();
}
Sign up to request clarification or add additional context in comments.

Comments

2

In this line:

if (arr[i][j] == arr[0][0] || arr[i][j] == arr[m][n]) {

You are testing for the equality of values within your array. You should be comparing whether the indices you are looking at are the beginning or end of the array. That is to say, you want to compare whether (in pseudo code):

i==0 and j==0, OR i==max index in its dimension and j==max index in its dimension

I have deliberately omitted the literal answer, because this looks a tiny bit like homework.

Comments

1

You compare the value of arr[i][j] with the value of arr[0][0]. You should instead compare i==0 && j==0 || i==m -1 && j==n -1

As your array was empty, and as you start the loop, arr[i][j] was equal to arr[0][0], skipping the first element. but for the next loop, arr[i][j] was still empty, and as you compare it to a non-initialised value, it's always true, skipping in each step

public static void main(String args[]) throws Exception {

    //Write code here
    Scanner sc = new Scanner(System.in);
    System.out.println("Input Rows: ");
    int m = sc.nextInt();
    System.out.println("Input Columns: ");
    int n = sc.nextInt();
    System.out.println("Enter values: ");
    int[][] arr = new int[m][n];
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (i==0 && j==0 || i==m-1 && j==n-1) {
                continue;
            } else {
                arr[i][j] = sc.nextInt();
            }
        }

        System.out.println();
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }

}

4 Comments

Id be careful with the order of operations. I suggest using parenthesis appropriately.
damn! such small mistake. Thanks a lot
Wrong, your condition can't be satisfied, as the loop invariants are i <m and j<n.
@ThomasTimbul thanks, i just remove the brackets forgetting the -1
-1

You don't need to check whether the array items are equal, you just want to check whether the row and column are equal to the last and first.

3 Comments

you can do that for first element but how can you do it for last element?
This is not correct. The OP wants to skip elements [0][0] and [m][n]. Your code skips all the [0][*], [*][0], [m][*], [*][n] elements.
Yeah, your'e right. I was read that wrong and I thought he wanted to skip the first and last row and columns. Will edit.

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.