我试图找出为什么我在NetBeans 7.2.1 IDE中创建的程序无法编译&在Notepad ++中运行。这对我来说很有意思,我想知道为什么会这样。
该程序具有主类SalaryDemo和另一个类Salary,其中包含程序的setter和getter。它运行正常,但计算错误除外。我很好奇这将如何在Notepad ++中运行,并且在设置Notepad ++后,我发现这不会运行。我试图对dos进行shell并且它做了同样的事情,所以我认为它不是Notepad ++。
该程序使用ArrayList作为销售代表和销售代表的年度总销售额。由于某种原因,VM无法找到ArrayList或Salary变量的符号。这是我的理解。我真的很感激这方面的帮助。这是我第一次对我这么轻松。
这是错误消息,如果您需要任何其他支持文档或代码,我会立即发布。
NPP_EXEC: "Compile and Run"
CD: Matches Current Directory
Current directory: Matches CD:
javac SalaryDemo.java
Process started >>>
SalaryDemo.java:37: error: cannot find symbol
ArrayList<Salary> salaryArray = new ArrayList<>();
^
symbol: class Salary
location: class SalaryDemo
SalaryDemo.java:37: error: unexpected type
ArrayList<Salary> salaryArray = new ArrayList<>()
^
required: class
found: <E>ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
SalaryDemo.java:52: error: cannot find symbol
Salary employee = new Salary();
^
symbol: class Salary
location: class SalaryDemo
SalaryDemo.java:52: error: cannot find symbol
Salary employee = new Salary();
^
symbol: class Salary
location: class SalaryDemo
SalaryDemo.java:103: error: cannot find symbol
Salary calcSalary = new Salary();
^
symbol: class Salary
location: class SalaryDemo
SalaryDemo.java:103: error: cannot find symbol
Salary calcSalary = new Salary();
^
symbol: class Salary
location: class SalaryDemo
6 errors
以下是相关代码
SalaryDemo (main class)
package salarydemofinal;
// Imports the DecimalFormat class and all java.util classes
import java.text.DecimalFormat;
SalaryDemo (main class)
// Public class SalaryDemo matches the filename and is accessable
// by methods outside the SalaryDemo class.
public class SalaryDemo
{
public static void main(String[] args)
{
// Initialize Scanner.
Scanner input = new Scanner(System.in);
// Initialize DecimalFormat to format percentages.
DecimalFormat df = new DecimalFormat("####%"); // Initialize
// Initialize the Array list and use the Salary class to store and
// manipulate elements of the array.
ArrayList<Salary> salaryArray = new ArrayList<Salary>();
String newEmployee = "";
double newSales = 0;
double counter;
double setSalesDifference;
double setTCompDifference;
double nqDifference;
Salary class
package salarydemofinal;
public class Salary {
// Initialize local Salary class variables
private String name; // Holds employee name
private double base = 4000; // Holds fixed monthly salary
private double sales; // Holds annual sales figure data-
private double calc; // Holds data calculate commission
private double com = .25; //Holds commission percentage multiplier
private double totalComp; // Holds sum of commission and annual salary data
private double annualSalary; // Holds data derived from 12 months of base rate
private double salesTarget = 120000;
private double threshold = salesTarget * .80;
private double commission; // sets the commission.
private double annualCompensation;
private double acceleratedSales;
private double notQualified;
这就是我在Notepad ++中运行compier的方式......我确实认识到这不符合常规,我觉得这应该有效。该计划并不复杂。
cd“$(CURRENT_DIRECTORY)” javac $(FILE_NAME) java $(NAME_PART)
在我看来,这是与shell进行shell并尝试编译和运行程序时使用的方法相同。此时,我真正知道的是,包中包含一个程序(项目)的所有必需类。 package语句需要在主类(SalaryDemo)和子类Salary中匹配。
我试图将它放在C:\\ salarydemofinal目录中,就像NetBeans IDE一样,并且不起作用。我知道我可能错过了一些基本的东西。正如我所提到的,这应该在我看来是有效的。
我考虑过这个问题并将Notepad ++排除在外,并试图编译它,因为我发现另一个stackoverflow文章建议所有的java文件需要立即编译,如javac * .java。我不知道这是不是积极的一步。错误数量减少到4,但这次没有找到Scanner。这是输出:
SalaryDemo.java:31:错误:找不到符号 扫描仪输入=新扫描仪(System.in); ^ 符号:类扫描仪 location:类SalaryDemo SalaryDemo.java:31:错误:找不到符号 扫描仪输入=新扫描仪(System.in); ^ 符号:类扫描仪 location:类SalaryDemo SalaryDemo.java:38:错误:找不到符号 ArrayList salaryArray = new ArrayList(); ^ symbol:类ArrayList location:类SalaryDemo SalaryDemo.java:38:错误:找不到符号 ArrayList salaryArray = new ArrayList(); ^ symbol:类ArrayList location:类SalaryDemo 4个错误
答案 0 :(得分:0)
您需要让Salary
类与SalaryDemo
位于同一个包中,或者您需要SalaryDemo
到import
。如果它们已经在同一个包中,则意味着您没有编译Salary
,并且您需要发布运行编译器的方式。
实际上并没有报告问题ArrayList
,只是Salary
。当您收到此错误时:
SalaryDemo.java:37: error: unexpected type
ArrayList<Salary> salaryArray = new ArrayList<>()
^
required: class
found: <E>ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
它告诉你的是参数化类型ArrayList<Salary>
不存在。但它不存在因为Salary
不存在。这只是早期错误的结果。