Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; import java.util.Arrays; import java.lang.Character; public cl

ID: 3598484 • Letter: I

Question

import java.io.*;
import java.util.Arrays;
import java.lang.Character;

public class Controller {
private char[][] myArray;

public void readWorld(Scanner in) {
// in this method i read off from a file and from that file I created my 2d char myarray
myArray = Arrays.copyOf(rowArray,n); // last line of my code

}
  
public String toString() {
return Arrays.deepToString(world);
}

public static void main(String[] args) {
SimpleBotController sbc = new SimpleBotController();
sbc.readWorld(new Scanner("www wrw www"));
System.out.println(sbc);
}
}

My problem is after running the code, its just giving me null. I think it's not reading myArray in the readWorld method. Any hints or advice on why its giving me null?

Explanation / Answer

Hi friend, since you have not posted the "reading part" of the code. I do not know how you are reading from Scanner object. Please find my sample code.

import java.io.*;

import java.util.Arrays;

import java.util.Scanner;

import java.lang.Character;

public class Controller {

  

   private char[][] myArray;

   public void readWorld(Scanner in) {

       // in this method i read off from a file and from that file I created my 2d char myarray

      

       myArray = new char[3][3];  

       int row = 0;

       while(in.hasNextLine()) {

           String line = in.next();

           char rowArray[] = new char[3];

           for(int i = 0; i<line.trim().length(); i++)

               rowArray[i] = line.charAt(i);

           myArray[row] = Arrays.copyOf(rowArray,3); // last line of my code

       }

      

   }

   public String toString() {

       return Arrays.deepToString(world);

   }

   public static void main(String[] args) {

       SimpleBotController sbc = new SimpleBotController();

       sbc.readWorld(new Scanner("www wrw www"));

       System.out.println(sbc);

   }

}