1

I have a java assignment that is asking for a code that tells the user to enter a point (row and column or x and y) and the program outputs all possible moves for the chess' knight.

package datastructureass1;
import java.util.Scanner;

public class DataStructureAss1 {

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

        int knight[][]= new int [7][7];

                     System.out.println("Please enter the knight's position starting with rows followed by columns");
                       int i=cin.nextInt();      
                       int j=cin.nextInt();

                        i=i+2;
                        j=j+1;
                        if (i<=8&&j<=8)

                           System.out.println( "{"+knight[i]+","+knight[j]+"}");

                        i= i+2;
                        j=j-1;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");

                        i= i-2;
                        j=j+1;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");
                        i= i-2;
                        j=j-1;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");   

                        i= i+1;
                        j=j+2;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");
                        i= i+1;
                        j=j-2;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");
                        i= i-1;
                        j=j+2;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");    
                        i= i-1;
                        j=j-2;
                        if (i<=8&&j<=8);
                            System.out.println( "{"+knight[i]+","+knight[j]+"}");

    }

                 }

when i run this in netbeans it gives me this kind of output:

{[I@6ac1abcf,[I@6ac1abcf}

{[I@50f6d9ca,[I@7e54864c}

{[I@6ac1abcf,[I@6ac1abcf}

{[I@5f3d285f,[I@7e54864c}

{[I@7e54864c,[I@2825a5d2}

{[I@6ac1abcf,[I@7e54864c}

{[I@7e54864c,[I@2825a5d2}

it doesnt make sense and i dont know what i did wrong in my code!

shmosel
  • 49,289
  • 6
  • 73
  • 138

6 Answers6

1

You have a two-dimensional array, that is an array of arrays:

[ 
  [ 0,1,2,3 ...], 
  [...], 
  [...]
]

When addressing a position in that array, you get the array on that position, i.e.

int[] knightAtI = knight[i];

As the int[] array is an object, the toString() method will return an reference string.

So you either have to access the field using both coordinates:

int knightAtIandJ = knight[i][j]

Or you convert the array at I to a String:

String knightAtI = Arrays.toString(knight[i])
Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
  • Good answer. Can you also explain what `[I@6ac1abcf` is? – Gray Sep 28 '16 at 18:47
  • It's the output of the default implementation of `toString()`, defined in `java.lang.Object`, which is _objectsClassName_ + @ + _objectsHashCodeAsHexString_ . `[I`is the class name of a primitive int array (try `int[].class.toString()`), – Gerald Mücke Sep 28 '16 at 18:52
1

knight[i] or knight[j] will point to an memory location of array since the knight is an 2D array. So you have to use knight[i][j] to print the value of the array.

Chanaka Lakmal
  • 1,112
  • 9
  • 19
1

Your array is meaningless. What did u want to do with it ? To print the positions you want, use :

System.out.println("(" + i + ";" + j + ")");
Bycob
  • 34
  • 3
0

You need to enclose the addition and subtraction operations for i and j in brackets for the if statements to only get executed on the condition

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

The code points to an array instead of array index ....use knight[i][j], due to which the hashcode of an array is getting printed instead of the actual value.

knight[i]      <-- it is an array
knight[i][j]   <-- it is j element in knight[i] array.
Rishi
  • 1,163
  • 7
  • 12
0

Most of your if-statements have a semicolon immediately after the closing parenthesis, thus the next line of code is outside the if-statement. Do as @Amer Qarabsa said and always put the body of the if-statement inside curly braces, and be very careful that you do NOT put a semicolon between the end-parenthesis and the opening brace.

FredK
  • 4,094
  • 1
  • 9
  • 11