vendredi 31 octobre 2014

Object Not Resolved to a Local Variable (Java)


Vote count:

0




I'm writing a Java program to process each state of a search graph, but can't seem to create instances of State inside one of my loops.


The program takes numbers from a file, line by line, and converts each number into a State object, however I'm getting an error State cannot be resolved to a variable



public class ABSrch {

static HashMap<Integer, State> states = new HashMap<Integer, State>();

public static void main(String[] args) {
File file = new File("D:\\Stuff\\TextExample.txt");
int level = 0, depthBound = 0, stateNumber = 0;

try {
Scanner s = new Scanner(file);

depthBound = Integer.parseInt(s.nextLine().split(" ")[1]);

s.close(); s = new Scanner(file);

while(s.hasNextLine()) {
String line = s.nextLine();
String[] tokens = line.split(" ");

if(level >= 0 && level <= 7) {
if(level == 0) tokens = line.split(" |7");
}
for(int i = 0; i < tokens.length; i++)
State parent = new State(false,0,0,null); //error: State not resolved to variable

level++;
}
s.close();
} catch (FileNotFoundException e) {
System.err.println("File not found");
}
}
}


State class:



public class State {
private boolean max;
private int name, value;
private State parent;
private ArrayList<State> children;

public State(boolean max, int name, int value, State parent) {
this.max = max;
this.name = name;
this.parent = parent;
children = new ArrayList<State>();
}

public boolean isMax() { if(max == true) return true; else return false; }
public void setMax(boolean max) { this.max = max; }

public int getValue() { return value; }
public void setValue(int value) { this.value = value; }

public int getName() { return name; }


}



asked 24 secs ago







Object Not Resolved to a Local Variable (Java)

Aucun commentaire:

Enregistrer un commentaire