簡體   English   中英

編譯錯誤:找不到符號

[英]Compile Error: Cannot Find Symbol

我試圖找出為什么我在NetBeans 7.2.1 IDE中創建的程序無法在Notepad ++中編譯和運行。 這只是令我着迷的事情,我想知道為什么會這樣。

該程序具有主類SalaryDemo和另一類Salary,其中包含該程序的設置方法和獲取方法。 除了計算錯誤外,它運行良好。 我對如何在Notepad ++中運行感到好奇,設置了Notepad ++之后,我發現它無法運行。 我試着去做dos,它做了同樣的事情,所以我不認為它是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 ++中運行編譯器的方式...我的確意識到這是不正常的,但是,我認為這應該可行。 該程序不是那么復雜。

cd“ $(CURRENT_DIRECTORY)” javac $(FILE_NAME)java $(NAME_PART)

在我看來,這與您在DOS中使用Shell並嘗試編譯並運行程序時使用的方法相同。 這時,我真正知道的是,軟件包包含一個程序(項目)的所有必需類。 package語句需要在主類(SalaryDemo)和子類Salary中都匹配。

我試圖像NetBeans IDE一樣將其放置在C:\\ salarydemofinal目錄中,但該方法不起作用。 我知道我可能缺少一些基本知識。 正如我所提到的,我認為這應該可行。

我考慮了一下,將Notepad ++從方程式中剔除,並炮轟到DOS嘗試進行編譯,因為我發現另一篇stackoverflow文章暗示所有Java文件都需要立即編譯,例如javac * .java。 我不知道這是否是積極的一步。 錯誤數量減少到4,但是這次沒有找到掃描儀。 這是輸出:

SalaryDemo.java:31:錯誤:找不到符號Scanner輸入= new Scanner(System.in); ^符號:類Scanner位置:類SalaryDemo SalaryDemo.java:31:錯誤:找不到符號Scanner輸入= new Scanner(System.in); ^符號:類Scanner位置:類SalaryDemo SalaryDemo.java:38:錯誤:找不到符號ArrayList salaryArray = new ArrayList(); ^符號:類ArrayList位置:類SalaryDemo SalaryDemo.java:38:錯誤:找不到符號ArrayList salaryArray = new ArrayList(); ^符號:類ArrayList位置:類SalaryDemo 4錯誤

您需要將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不存在。 這只是先前錯誤的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM