简体   繁体   English

为什么我不断收到找不到符号的错误

[英]Why do i keep getting a cant find symbol error

I am trying to store one of three items a book, cd, or dvd into an array but my add method is not working. 我正在尝试将书籍,cd或dvd的三个项目之一存储到数组中,但是我的add方法不起作用。

here is the GUI Class. 这是GUI类。

/**
    Class BookStoreApplication GUI represents a book store.
    It gives the user the option to
        - add a book to the store's inventory
        - list all books in the store's inventory


     Author: YOUR FULL NAME HERE
     E-mail address: YOUR E-MAIL ADDRESS
     Last changed: TODAY'S DATE
     Lab 01
*/

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TextArea;

import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;

import javafx.geometry.Pos;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;


public class BookStoreGUI extends Application {



    private String title;
    private String author;
    private double price;
    private static int pages;
    private static int playingTime;

    private Label titleLabel;
    private Label authorLabel;
    private Label priceLabel;

    private TextField titleTextField;
    private TextField authorTextField;
    private TextField priceTextField;

    private GridPane inputGrid;

    private RadioButton BookRadioButton;
    private RadioButton CDRadioButton;
    private RadioButton DVDRadioButton;

    private VBox RadioVBox;

    private ToggleGroup mediaGroup;

    private Button addItemButton;
    private Button displayInventoryButton;

    private HBox buttonHBox;

    private TextArea outputText;

    private BookStoreHandler handler;

    private Stage maessageWindow;

    private GridPane topGrid;

    private BorderPane mainPane;


    private static int count = 0;

    private static BookStoreItem[] Catalog; 




    public void start(Stage primaryStage) {


      createInputComponents();
      createOutputComponents();
      createButtons();
      createWindow();

      Scene scene = new Scene(mainPane, 400, 500);

      primaryStage.setTitle("Book Sotre");

      primaryStage.setScene(scene);

      primaryStage.show();
  }

   private void createInputComponents(){

    titleLabel = new Label("Title");
    authorLabel = new Label("Author's name");
    priceLabel = new Label ("Price");

    titleTextField = new TextField();
    authorTextField = new TextField();
    priceTextField = new TextField();

    inputGrid = new GridPane();
    inputGrid.setHgap(15);
    inputGrid.setVgap(15);

    inputGrid.add(titleLabel, 0 , 0);
    inputGrid.add(titleTextField, 1, 0);
    inputGrid.add(authorLabel, 0, 1);
    inputGrid.add(authorTextField, 1, 1);
    inputGrid.add(priceLabel, 0 , 2);
    inputGrid.add(priceTextField, 1, 2);

    CDRadioButton = new RadioButton("CD");
    BookRadioButton = new RadioButton("Book");
    DVDRadioButton = new RadioButton("DVD");

    mediaGroup = new ToggleGroup();

    CDRadioButton.setToggleGroup(mediaGroup);
    BookRadioButton.setToggleGroup(mediaGroup);
    DVDRadioButton.setToggleGroup(mediaGroup);

    BookRadioButton.setSelected(true);
    RadioVBox = new VBox(15);

    RadioVBox.getChildren().add(CDRadioButton);
    RadioVBox.getChildren().add(BookRadioButton);
        RadioVBox.getChildren().add(DVDRadioButton);

        topGrid = new GridPane();
        topGrid.setHgap(20);
        topGrid.setAlignment(Pos.CENTER);

        topGrid.add(inputGrid, 0, 0);
        topGrid.add(RadioVBox, 1, 0);


  }
  private void createOutputComponents () {

       outputText = new TextArea();

       outputText.setEditable(false);
  }

  private void createButtons() {

    addItemButton = new Button("Add Item");
    displayInventoryButton = new Button("Display Inventory");

    addItemButton.setPrefSize(100, 50);
    displayInventoryButton.setPrefSize(100, 50);

    buttonHBox = new HBox(100);

    buttonHBox.setAlignment(Pos.CENTER);

    buttonHBox.getChildren().add(addItemButton);
    buttonHBox.getChildren().add(displayInventoryButton);

    handler = new BookStoreHandler();

    addItemButton.setOnAction(handler);
    displayInventoryButton.setOnAction(handler);
  }

 private void createWindow() {

       mainPane = new BorderPane();

       mainPane.setTop(topGrid);
       mainPane.setCenter(outputText);
       mainPane.setBottom(buttonHBox); 
   }

    private class BookStoreHandler implements EventHandler<ActionEvent> {
        public void handle(ActionEvent ae) {
            BookStoreItem media;
            if(ae.getSource () == addItemButton){
                outputText.setText("");

                title = titleTextField.getText().trim();
                author = authorTextField.getText().trim();
                price = Double.parseDouble(priceTextField.getText().trim());

                if (CDRadioButton.isSelected()){
                    media = new CD(title, author, price, 0);

                }
                else if(DVDRadioButton.isSelected()) {
                    media = new DVD(title, author, price, 0);
                }
                else {
                    media = new Book(title, author, price, 0);
                }

                Catalog = Catalog.add(media);




           }

       }
   }
}

here is my error message. 这是我的错误信息。 BookStoreGUI.java:210: error: cannot find symbol Catalog = Catalog.add(media); BookStoreGUI.java:210:错误:找不到符号Catalog = Catalog.add(media); ^ symbol: method add(BookStoreItem) location: variable Catalog of type BookStoreItem 1 error ^符号:方法add(BookStoreItem)位置:变量BookStoreItem类型的目录1错误

and her is the catalog class from wich the add method comes from. 她是add方法来自的目录类。

  public class Catalog {

    private BookStoreItem[] inventory;
    private final int CAPACITY = 100;
    private int count;

    public Catalog() {
        inventory = new BookStoreItem[CAPACITY];
        count = 0;
    }

    public void add(BookStoreItem newItem) {
        inventory[count] = newItem;
        count++;
    }

     public boolean isAvailable(String title) {

        boolean found = false;

         for (int i = 0;i < count && !found;i++) {
             if (title.equals(inventory[i].getTitle())) {
                found = true;
            }
        }

        return found;
    }

    public BookStoreItem getItem(String title) {

        BookStoreItem desiredItem = null;

        boolean found = false;

         for (int i = 0;i < count && !found;i++) {
            if (title.equals(inventory[i].getTitle())) {
                desiredItem = inventory[i];
                found = true;
            }
        }

         return desiredItem;

    }

    public BookStoreItem[] getList() {
         return inventory;
    }

}

Firstly, you would have fewer problems if you followed Java naming conventions - currently Catalog is both an array variable name and a type name. 首先,如果遵循Java命名约定,则问题会更少-当前Catalog既是数组变量名又是类型名。

Secondly, there is no add method in arrays. 其次, 没有 add的阵列方法。 You may want a List , eg ArrayList . 您可能需要一个List ,例如ArrayList Alternatively, you may well just need an instance of Catalog , eg 另外,您可能只需要Catalog的实例,例如

// This replaces your private static BookStoreItem[] Catalog; declaration
Catalog catalog = new Catalog();

Then you can use this in your handle method: 然后可以在handle方法中使用它:

catalog.add(media);

Note that I've made it an instance variable rather than a static variable - I doubt that you want any static variables, to be honest. 请注意,我将其设置为实例变量而不是静态变量-老实说,我怀疑您是否需要任何静态变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么为什么不断出现“找不到符号”错误? - Why do I keep getting “cannot find symbol” error? 为什么会不断出现错误:找不到变量“重量”和“行星”的符号? - Why do I keep getting the error: cannot find symbol for variables “weight” and “planet”? 为什么我不断收到缺少符号的编译错误? - Why do I keep getting a compile error for a missing symbol? JAVA:为什么继续出现错误:尝试访问另一个类文件时找不到符号? - JAVA: Why do I keep getting the error: cannot find symbol when I try to access another class file? 为什么我不断在代码中找不到符号方法错误? - Why do I keep getting cannot find symbol method errors in my code? 为什么我的程序没有用? 不断获取找不到符号错误? - Why my program no work ? keep getting cannot find symbol error? 我不断收到错误消息:无法找到或加载主类,无法找出原因? - I keep getting Error: Could not find or load main class and cant figure out why? 无法弄清楚为什么我不断收到找不到符号编译错误 - Can't figure out why i keep getting cannot find symbol compile error 尝试进行构造函数链接时,我不断收到“错误:找不到符号” - I keep getting “error: cannot find symbol” when trying to do constructor chaining 为什么我不断收到错误无法解析符号片段活动? - why do I keep getting the error Cannot resolve symbol Fragment activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM