Reputation: 19
In the example above, in the top method (initialiseGame) I have created a file named boardDEFAULT if the first argument is "DEFAULT". Now I want to call that file in a method below (printBoard) in order to read the out.println lines established in the top method.
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class Example {
public static void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0];
if ("DEFAULT".equals(configFileName)) {
try {
File boardDEFAULT = new File("easy_board.txt");
PrintWriter output = new PrintWriter(boardDEFAULT);
output.println("######");
output.println("#@ &2#");
output.println("## ##");
output.close();
} catch (IOException ex) {
System.out.println("Error");
return; }
}
}
public static void printBoard() {
if ("DEFAULT".equals(boardDEFAULT)) {
String [][] DefaultBoardArray = new String [6][3];
DefaultBoardArray[0][0] = "#";
DefaultBoardArray[1][0] = "#";
DefaultBoardArray[2][0] = "#";
DefaultBoardArray[3][0] = "#";
DefaultBoardArray[4][0] = "#";
DefaultBoardArray[5][0] = "#";
DefaultBoardArray[0][1] = "#";
DefaultBoardArray[1][1] = "@";
DefaultBoardArray[2][1] = " ";
DefaultBoardArray[3][1] = "&";
DefaultBoardArray[4][1] = "2";
DefaultBoardArray[5][1] = "#";
DefaultBoardArray[0][2] = "#";
DefaultBoardArray[1][2] = "#";
DefaultBoardArray[2][2] = " ";
DefaultBoardArray[3][2] = "#";
DefaultBoardArray[4][2] = "#";
DefaultBoardArray[5][2] = "#";
for (int i=0; i<7; i++) {
for (int j=0; j<3; j++) {
System.out.print(DefaultBoardArray[i][j] + " ");
}
}
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Error: Too few arguments given. Expected 1 argument, found "+args.length+".");
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
return;
}
if (args.length >1) {
System.out.println("Error: Too many arguments given. Expected 1 argument, found "+args.length+".");
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
return;
}
Scanner keyboard = new Scanner (System.in);
String [] Command = new String [2];
Command[0] = keyboard.nextLine();
while (!("help".equalsIgnoreCase(Command[0])) || !("board".equalsIgnoreCase(Command[0])) || !("status".equalsIgnoreCase(Command[0])) || !("left".equalsIgnoreCase(Command[0])) || !("right".equalsIgnoreCase(Command[0])) || !("up".equalsIgnoreCase(Command[0])) || !("down".equalsIgnoreCase(Command[0])) || !("save <file>".equalsIgnoreCase(Command[0]))) {
if (!("help".equalsIgnoreCase(Command[1])) || !("board".equalsIgnoreCase(Command[1])) || !("status".equalsIgnoreCase(Command[1])) || !("left".equalsIgnoreCase(Command[1])) || !("right".equalsIgnoreCase(Command[1])) || !("up".equalsIgnoreCase(Command[1])) || !("down".equalsIgnoreCase(Command[1])) || !("save <file>".equalsIgnoreCase(Command[1]))) {
System.out.println("Error: Could not file command '"+Command[1]+"'.");
} else {
System.out.println("Error: Could not file command '"+Command[0]+"'."); }
System.out.println("To find the list of valid commands, please type 'help'.");
Command[1] = keyboard.nextLine();
if (("help".equalsIgnoreCase(Command[1])) || ("board".equalsIgnoreCase(Command[1])) || ("status".equalsIgnoreCase(Command[1])) || ("left".equalsIgnoreCase(Command[1])) || ("right".equalsIgnoreCase(Command[1])) || ("up".equalsIgnoreCase(Command[1])) || ("down".equalsIgnoreCase(Command[1])) || ("save <file>".equalsIgnoreCase(Command[1]))) {
break;
}
}
if (("board".equalsIgnoreCase(Command[0])) || ("board".equalsIgnoreCase(Command[1]))) {
printBoard();
}
}
}
In turn I get an error saying
error: cannot find symbol - if ("DEFAULT".equals(boardDEFAULT))
With a hat pointing at boardDEFAULT.
I don't know how to accomplish this I have tried creating an array of files but that led to more problems. I am a bit of a novice at methods, I apologise for this elementary question.
Upvotes: 0
Views: 30
Reputation: 11620
Although this is a nasty way, you need to use a global variable (static) to pass the value from one method to another.
public class Example {
private static isDefualtConfig = false; // value shared by both methods
public static void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0];
if ("DEFAULT".equals(configFileName)) {
isDefualtConfig =true;
...
}
public static void printBoard() {
if (isDefualtConfig) {
String [][] DefaultBoardArray = new String [6][3];
...
Generally static methods are hard to work with. Your example does not compile because parameter from one static method is not visible in another static method. So you need to create a flag, that will be used as sort of cache between them.
This has one downside. Your initialiseGame
method is global for all Example
class instances.
Upvotes: 0
Reputation:
You probably want something like this:
public class Example {
private boolean isDefault = false; // class member => available in all non static methods
public void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0]; // NB: this makes no sense
if ("DEFAULT".equals(configFileName)) {
isDefault = true;
// ...
public void printBoard() {
if (this.isDefault) {
// ...
public static void main(String[] args) {
Example example = new Example();
// use example: example.initialiseGame(...);
}
Some reading: https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
(Also note that in your code boardDEFAULT
is a File
so it will never be equal to "DEFAULT"
)
Upvotes: 1