Java: Can any one tell me why I have red lines for the value() and valueOf() and
ID: 3881296 • Letter: J
Question
Java: Can any one tell me why I have red lines for the value() and valueOf() and how I can fix it where the *** is at beginning of line.
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
public class Direction {
public ArrayList<Direction> synonyms;
public enum Synonyms {
//Values
N, S, E, W, NE, NW, SE, SW, UP, DOWN, UNKNOWN;
//Instance variable
ArrayList<Direction> synonyms;
//Default constructor
private Synonyms() {
}
}
//Class constructor
public Direction() {
synonyms = new ArrayList<>();
try {
//Read properties file
FileReader reader = new FileReader("direction.properties");
//Properties object
Properties p = new Properties();
//Load properties from file
p.load(reader);
//Get properties value
String strDirection = p.getProperty("direction");
//Check if enum contains value or not
if (Arrays.asList(Direction.values()).contains(strDirection)) {
//Add value in arraylist
synonyms.add(Direction.valueOf(strDirection));
}
} catch (Exception e) {
System.err.println(e);
}
}
public static void main(String[] args) {
//Create object
Direction ex = new Direction();
//Print Array
for (Direction dir : ex.synonyms) {
System.out.println(dir);
}
}
}
Explanation / Answer
you are getting error because values() and valueOf() methods belongs to enum class. You are using Direction class for both method, if you use Synonyms which is enum class, you won't get any issue for these two methods.
Synonyms.values()
Synonyms.valueOf()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.