I am creating a directed graph that represents a maze. One of the mechanics in the maze is to flip the direction of nodes. I am just reading the nodes from a file and I want to change the direction of node so it is opposite. By direction I mean N,W,S etc. However, my function does not work when using the String from the scanner and returns null when doing a system out. But does work when putting in a made up string, for example String test = "E" and then test = flipDirection(test). This sets test to "W". So I know that the flipDirection function and the String compares are valid. The output for the first line is "E" and then null.
public Digraph(){
FileReader reader = null;
Scanner scan = null;
try {
reader = new FileReader("input.txt");
scan = new Scanner(reader);
scan.nextLine();
while(scan.hasNextLine()){
//read in unflipped nodes
int row = scan.nextInt();
int col = scan.nextInt();
char color = scan.next().charAt(0);
String circle = scan.next();
String direction = scan.nextLine();
//add a node to the graph
Vertex v1 = new Vertex(row, col, color, circle, direction);
addNode(v1);
//add a flipped node
System.out.println(direction);
direction = flipDirection(direction);
System.out.println(direction);
Vertex v2 = new Vertex(row, col, color, circle, direction);
addNode(v2);
}
} catch (FileNotFoundException e) {
System.out.println(e.getLocalizedMessage());
}
}
public static String flipDirection(String d){
String flip = null;
if(d.equals("N"))
flip = "S";
if(d.equals("S"))
flip = "N";
if(d.equals("E"))
flip = "W";
if(d.equals("W"))
flip = "E";
if(d.equals("NW"))
flip = "SE";
if(d.equals("SE"))
flip = "NW";
if(d.equals("NE"))
flip = "SW";
if(d.equals("SW"))
flip = "NE";
return flip;
}
public class Vertex {
//name of vertex and a pointer to the first node in its adj linked list
public int row;
public int col;
public char color;
public String direction;
public String isCircle;
public Vertex(int row, int col, char color, String isCircle, String direction) {
super();
this.row = row;
this.col = col;
this.color = color;
this.isCircle = isCircle;
this.direction = direction;
}
@Override
public String toString() {
return "Vertex [row=" + row + ", col=" + col + ", color=" + color
+ ", direction=" + direction + ", isCircle=" + isCircle + "]";
}
And this is what the input file looks like, without the extra spaces. The direction is the last part of text in each line
7 7
1 1 R N E
1 2 B N W
1 3 B N NW
1 4 R N NW
1 5 R N S