Results 1 to 5 of 5
- 03-14-2011, 07:00 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Passing 2 dementional array to a new class
Hello everyone, first post =D
Okay, so I am writing a program (which is an assignment for school) that reads a maze from an input file (or creates a random maze of any size) and then searches that maze and prints out the correct path (if there is one) to traverse the maze (as well as the coordinates that it took along its path).
I have just finished writing my read maze from file, create random maze, and print maze to screen (as well as all the user interaction stuff). All i have left to do is the searching part, which I plan on writing in a different class because it is such a major (and likely large) part of the final program.
My question does not lie with any of this, but rather how to correctly pass a variable from one class to another. My code is as follows, and I will highlight the relevant parts of the code.
"MANAGER" class:
Java Code:import java.util.*; import java.io.*; public class Manager { Scanner reader, input; String[][] maze; String command; int cols, rows;[COLOR="Red"] searchMaze search;[/COLOR] [COLOR="Red"] public Manager() { search = new searchMaze(); input = new Scanner(System.in); }[/COLOR] public String getCommand() { System.out.print("enter command: "); return input.next(); } public void readMaze(String fileName) //saves maze from file to 2d array "maze" { boolean opened; boolean setSize = false; boolean wrongSize = false; boolean blockReached = false; int rowVerify = 0; int colVerify = 0; File infile = new File(fileName); try { reader = new Scanner(infile); opened = true; } catch (FileNotFoundException e) { opened = false; } if (opened == true) { String first = reader.next(); String second = reader.next(); if (isInt(first) == true && isInt(second) == true) { rows = Integer.parseInt(first); cols = Integer.parseInt(second); maze = new String[rows][cols]; setSize = true; } String currentLine = ""; int currentRow = 0; int currentCol = 0; while (reader.hasNext() && setSize == true) { String currentString = reader.next(); currentLine = currentLine + currentString + " "; // concatinate string untill the next if-statement stops the process if (currentLine.length() > 2*cols + 2) // if true, then array will be too small to be filled completely. IE: the user gave incorrect { // row/col values. Breaks loop here and is dealt with below. wrongSize = true; break; } else if (currentLine.length() == (2*cols + 2)) // "2*cols + 2" is the number of characters contained in a maze of any size { // | |x| | | <<-- 4 cols, 4*2 + 1 = total chars = 9. the reason that it is + 2 and not + 1 // is due to the concatination of one extra white space for (int i = 1; i < 2*cols + 1; i = i + 2) { char item = currentLine.charAt(i); if (currentRow >= rows || currentCol >= cols) // if either currentRow or currentCol exceeds its respective fixed array value, then break; // there is a sizing issue, and an error will eventually culminate. This error is SIZE // based, and is caught pre-emptively and delt with below. This ultimately means that maze[currentRow][currentCol] = Character.toString(item); currentCol++; } currentRow++; // the current file line means new row rowVerify = currentRow; colVerify = currentCol; //necisary for comparison after loop has ended, due to the statement "currentCol = 0;" currentCol = 0; //reset colums for new row currentLine = ""; // reset concatination variable for new line in file } } if (colVerify != cols || rowVerify != rows) wrongSize = true; } if (opened == false) System.out.println("File could not be opened. Ensure maze file exists and is in accessible folder."); if (setSize == false) System.out.println("Maze size not found. Ensure maze size is the first item in maze file in format \"<n> <m>\""); if (wrongSize == true || blockReached == false) { System.out.println("Given maze does not fit the specified size. Fix maze size and try again (No maze was saved because of size issues. Fix formatting and try again.)"); cols = 0; rows = 0; maze = new String[rows][cols]; } reader.close(); } public void show() // prints maze to screen { for (int i = 0; i < rows; i++) { System.out.println(); for (int j = 0; j < cols; j++) { if (j == 0) System.out.print("|"+maze[i][j]+"|"); else System.out.print(maze[i][j]+"|"); } } System.out.println(); System.out.println(); } public void makeRandomMaze(int tempRows, int tempCols) // creates random n x m maze saved as 2d array "maze" { Random randomGen = new Random(); rows = tempRows; cols = tempCols; maze = new String[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int randomInt = randomGen.nextInt(3); // 1/4 probability of X if (randomInt == 0 && (i + j) != 0 && (i + j) != (rows + cols - 2)) // Prevents X from being placed at enterance position (0,0) maze[i][j] = "X"; else maze[i][j] = " "; } } } [COLOR="Red"] public String[][] passMaze() { return maze; }[/COLOR] public void run() // contains all user-interaction logic { showMenu(); command = getCommand(); while (!command.equals("exit")) { [COLOR="Red"] if (command.equals("make")) { String n = input.next(); String m = input.next(); if (isInt(n) == true && isInt(m) == true) { makeRandomMaze(Integer.parseInt(n), Integer.parseInt(m)); search.getMaze(); }[/COLOR] else System.out.println("Invalid row/column combination. Ensure both values given are integers and try again."); } else if (command.equals("read")) { String mazeFile = input.next(); readMaze(mazeFile); } else if (command.equals("search")) { System.out.println("method does not exist yet"); } else if (command.equals("show")) show(); else if (command.equals("help")) showMenu(); else System.out.println("Command not found. Try again."); command = getCommand(); } } public void showMenu() //command options { System.out.println(); System.out.println("make <n> <m> // make a new randomly generated maze with n rows and m columns"); System.out.println("read <filename> // read a maze from a text file"); System.out.println("search // find a path through the current maze"); System.out.println("show // display the current maze on the screen"); System.out.println("help // display menu"); System.out.println("exit // close program"); System.out.println(); } public boolean isInt(String item) //Generic pre-writen method to verify if a String is an Integer { int intCount = 0; char chars[] = item.toCharArray(); for (int i = 0; i < chars.length; i++) { char currentChar = chars[i]; if (Character.isDigit(currentChar)) intCount = intCount + 1; } if (intCount == chars.length) return true; else return false; } public static void main(String args[]) { Manager driver = new Manager(); driver.run(); } }
"SEARCH MAZE" class:
The code in the searchMaze class's getMaze method is of course not what I am ultimately going to do, but if you guys can help me get that code to work, then I will be able to continue writing my code. The getMaze method was simply created because I did not think that it would work, so I wanted to test it before moving on.Java Code:public class searchMaze { String[][] mazeToSearch; [COLOR="Red"] Manager manager;[/COLOR] public void getMaze() { [COLOR="Red"]Manager main = new Manager(); System.out.println(mazeToSearch); //prints null, which is expected. mazeToSearch = main.passMaze(); System.out.println(mazeToSearch); //still prints null...?[/COLOR] } }
If anyone knows what is wrong with this "passing" aspect of my code, any help would be very much appreciated.
Also, if you don't know what is specifically wrong with my code, but know how to fix my problem, that's completely fine with me =S just want to get it figured out so i can finish this up.
Thanks in advance,
anonymous
PS: I am only allowed to use the very basic built in methods/class, as this is an introductory course to data structures...they want us to do everything (within reason) by hand.
- 03-15-2011, 04:55 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 18
- Rep Power
- 0
In your "searchMaze" class, you have created a new Manager class in which the maze is not initialized. When you call passMaze() you are only getting the maze, nothing happens to it, which is why it is null. If you call makeRandomMaze() or readMaze() before you call passMaze() the maze array will be initialized and you would get some output.
Java Code:public class searchMaze { String[][] mazeToSearch; Manager manager; public void getMaze() { Manager main = new Manager();[COLOR="Red"]//maze in this class is null[/COLOR] System.out.println(mazeToSearch); //prints null, which is expected. mazeToSearch = main.passMaze();[COLOR="Red"]//getting the maze from manager class, which is still null[/COLOR] System.out.println(mazeToSearch); } }Last edited by mr_guy; 03-15-2011 at 04:59 AM.
- 03-15-2011, 09:55 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Hey, thanks for the reply. I figured that much out (about the initialization creating an instance of the class where the object wasnt created) and just declared the variable public static.
Your method is what I was looking for, but i just built the rest of my program around what I had done.
Great to know regardless.
Thanks.
-
- 03-15-2011, 11:07 PM #5
Similar Threads
-
passing a .txt into an array
By j_rod722 in forum New To JavaReplies: 6Last Post: 02-07-2011, 11:45 PM -
Passing array to model class
By keshaba in forum IntelliJ IDEAReplies: 0Last Post: 04-20-2010, 01:43 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
Passing a 2D array
By toymachiner62 in forum New To JavaReplies: 6Last Post: 10-23-2009, 04:05 PM -
passing text from a class to a gui class
By rob in forum AWT / SwingReplies: 2Last Post: 02-13-2009, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks