java - Getting an ouput of null on 2d array not sure where I went wrong -
i have problem in programing class , stuck @ point im not sure fix.
here problem:
the world class contain 3 private data fields. first private data field 2 dimensional char array represents world. size of world depend on what’s passed constructor. next private data field characterrow , last private data field charactercolumn. constructor accept 2 values, width , height. using width , height, set size of 2 dimensional array. fill array dash, -. characterrow , charactercolumn data fields both set 0 , place character, ‘p’, @ position characterrow , charactercolumn. if world printed width 9, or 9 columns, , height 4, or 4 rows, after creation:
p-------- --------- --------- ---------
the world class contain 5 public methods. these methods moveup, movedown, moveleft, moveright , displayworld.
i getting ouput of:
enter number of rows: 9 enter number of columns: 9 p - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - null - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - commands can up, down, left, right, or exit. exit closes program. enter command:
i can points user enters size of array array prints null character @ correct location prints again correctly no character. unsure have gone off track here. can please take , me in way? code both classes below
driver class:
import java.util.scanner; public class driver { public static void main(string[] args) { world world = new world(); system.out.println("commands can up, down, left, right, or exit. exit closes program."); system.out.println("enter command: "); scanner input = new scanner(system.in); while (true) { string s = input.nextline(); switch (s) { case "up": world.moveup(); world.displayworld(); break; case "down": world.movedown(); world.displayworld(); break; case "left": world.moveleft(); world.displayworld(); break; case "right": world.moveright(); world.displayworld(); break; case "exit": break; } } } }
world class:
import java.util.*; public class world { private static final string p = "p"; private string[][] array; public world() { scanner input = new scanner(system.in); system.out.println("enter number of rows: "); int crow = input.nextint(); system.out.println("enter number of columns: "); int ccol = input.nextint(); array = new string[crow][ccol]; array[0][0]=p; displayworld(); for(int = 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { array[i][j] = new string(); } } displayworld(); } public void displayworld() { system.out.println(); for(int = 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { system.out.print(array[i][j] + " - "); } system.out.println(); } } public void moveup() { for(int i= 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { if ((array[i][j]) == " - ") { if (i < array.length - 1) { array[i][j] = " - "; array[i - 1][j] = p; } return; } } } } public void movedown() { for(int i= 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { if ((array[i][j]) == " - ") { if (i < array.length - 1) { array[i][j] = " - "; array[i + 1][j] = p; } return; } } } } public void moveleft() { for(int i= 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { if ((array[i][j]) == " - ") { if (i < array.length - 1) { array[i][j] = " - "; array[i][j - 1] = p; } return; } } } } public void moveright() { for(int i= 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { if ((array[i][j]) == " - ") { if (i < array.length - 1) { array[i][j] = " - "; array[i][j + 1] = p; } return; } } } } }
public world(){ scanner input = new scanner(system.in); system.out.println("enter number of rows: "); int crow = input.nextint(); system.out.println("enter number of columns: "); int ccol = input.nextint(); array = new string[crow][ccol]; //array[0][0]=p; //displayworld(); for(int = 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) { if(i == 0 && j == 0){ array[i][j] = p; }else{ array[i][j] = new string(); } } } displayworld(); }
in constructor, have displayworld method call before filling array. then, when filling it, re-fill position [0][0] , p disappears. using code it's above, you'll want.
i hope have helped you!
Comments
Post a Comment