英文:
Java cannot find enum variable
问题
I am having issues calling the Severity.LOW
variable from the Enum
I created. I have tried importing it, wrapping it in a class, and importing the wrapper class. I cannot figure out what I'm doing wrong.
Here is the Main.java
:
package allergyProblem;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Name:");
String firstName = sc.nextLine();
System.out.println("Enter Last Name:");
String lastName = sc.nextLine();
System.out.println("Enter Phone Number:");
String phoneNumber = sc.nextLine();
System.out.println("Enter E-Mail Address:");
String emailAddress = sc.nextLine();
System.out.println("Enter Street Name:");
String streetName = sc.nextLine();
System.out.println("Enter City:");
String city = sc.nextLine();
System.out.println("Enter State:");
String state = sc.nextLine();
System.out.println("Enter Zipcode:");
int zipCode = sc.nextInt();
Address adr = new Address(streetName, city, state, zipCode);
Allergy allergy = new Allergy("coughing", "Arzoo", Severity.LOW, "Regular cough");
List<Allergy> allergies = new ArrayList<Allergy>();
allergies.add(allergy);
Patient patient = new Patient(firstName, lastName, phoneNumber, emailAddress, adr, allergies);
System.out.println(patient);
}
}
Here is the Enum
I'm trying to access:
package allergyProblem;
import java.util.*;
public enum Severity {HIGH, MEDIUM, LOW}
This is the error I get when trying to compile Main.java
.
Severity.java
, which is the Enum
, compiled properly without any errors:
Main.java:43: error: cannot find symbol
Allergy allergy = new Allergy("coughing", "Arzoo", Severity.LOW, "Regular cough");
^
symbol: variable LOW
location: class Severity
1 error
英文:
I am having issues calling the Severity.LOW
variable from the Enum
I created. I have tried importing it, wrapping it in a class, and importing the wrapper class. I cannot figure out what im doing wrong.
Here is the Main.java
package allergyProblem;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Name:");
String firstName = sc.nextLine();
System.out.println("Enter Last Name:");
String lastName = sc.nextLine();
System.out.println("Enter Phone Number:");
String phoneNumber = sc.nextLine();
System.out.println("Enter E-Mail Address:");
String emailAddress = sc.nextLine();
System.out.println("Enter Street Name:");
String streetName = sc.nextLine();
System.out.println("Enter City:");
String city = sc.nextLine();
System.out.println("Enter State:");
String state = sc.nextLine();
System.out.println("Enter Zipcode:");
int zipCode = sc.nextInt();
Address adr = new Address(streetName, city, state, zipCode);
Allergy allergy = new Allergy("coughing", "Arzoo", Severity.LOW, "Regular cough");
List<Allergy> allergies = new ArrayList<Allergy>();
allergies.add(allergy);
Patient patient = new Patient(firstName, lastName, phoneNumber, emailAddress, adr, allergies);
System.out.println(patient);
}
}
Here is the Enum
im trying to access
package allergyProblem;
import java.util.*;
public enum Severity {HIGH, MEDIUM, LOW}
This is the error i get when trying to compile Main.java
.
Severity.java
which is the Enum
, compiled properly without any errors.
Main.java:43: error: cannot find symbol
Allergy allergy = new Allergy("coughing", "Arzoo", Severity.LOW, "Regular cough");
^
symbol: variable LOW
location: class Severity
1 error
答案1
得分: 2
错误提示您 Severity
作为一种类型被找到(忽略它说的是 'class Severity',即使 javac 知道它是一个枚举,不幸的是它会这样做),但是它不包含变量 LOW
。
只有一个解释:
在这里 javac 使用的 Severity
并不是 编译 public enum Severity {HIGH, MEDIUM, LOW}
的结果。请检查一下在您的类路径中是否有其他名为 Severity 的类,或者在包含您的 Main.java
文件的包中是否有。然后,检查一下是否正确(重新)编译了 Severity.java,因为旧的类文件也可能会引起问题。然后检查一下类路径上是否有一些旧的构建结果。
注意:即使在修复此问题后,您的代码也无法正常工作,您滥用了 Scanner。使用 scanner 的正确方法要么只调用 .nextLine
,要么根本不调用它,强烈建议不要调用它。如果要读取行,请在创建 scanner 后立即运行 scanner.useDelimiter("\r?\n");
,以便告诉 scanner 您希望每次回车输入一个标记,而不是每个空白字符输入一个标记(无论何时接受命令行输入,scanner 默认使用空白字符作为分隔符都是一个错误,由于向后兼容性的考虑,无法修复这个问题。养成一个好习惯:创建 scanner 吗?立即设置分隔符)。然后,您只需调用 next()
而不是 nextLine()
来获取一行的数据,然后它就能正常工作 - 由于众所周知的在使用 nextLine 和 nextAnythingElse 时会出现问题,您的方法将无法工作。
英文:
The error is telling you that Severity
as a type is found (ignore that it says 'class Severity', it does that even if javac knows it is an enum, unfortunately), but that it does not contain the variable LOW
.
There's only one explanation for that:
The Severity
that javac is using here is NOT the result of compiling public enum Severity {HIGH, MEDIUM, LOW}
. Check if there are other classes named Severity in your classpath in that package, or in the package that contains your Main.java
file. Then, check that you properly (re)compiled Severity.java, because a stale class file can also mess with it. Then check that you don't have some old stale build result on the class path.
NB: Note that your code won't work even after fixing this, you're abusing Scanner. The right way to use scanner is to either only call .nextLine, or to never call it, with a strong preference for never calling it. If you want to read lines, after making the scanner, immediately run scanner.useDelimiter("\r?\n");
in order to tell the scanner that you want one token per enter, not one token per whitespace (this is a good idea in any case when taking command line input, the fact that scanner defaults to whitespace as delimiter is a dumb error that cannot be fixed due to backwards compatibility concerns. It's a good habit to get into: Making a scanner? Immediately set the delimiter). Then, you can just call next()
instead of nextLine()
for a lines worth of data, and then it will work - your take will not work due to well known issues mixing nextLine and nextAnythingElse.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论