[英]How to access ArrayList in another class
I have this code currently but it wont let me run it as there is an error, I know it is something to do with how I have imported the Library class however I do not know the appropriate way to fix this. 我目前有这段代码,但是由于出现错误它不会让我运行它,我知道这与我导入Library类的方式有关,但是我不知道解决此问题的适当方法。 I am trying to import the ArrayList to use when loading a save file. 我正在尝试导入ArrayList,以在加载保存文件时使用。 I want this ArrayList to be updated from that in the text file. 我希望从文本文件中更新该ArrayList。
Here is my code: 这是我的代码:
package library;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Manual {
String serial;
String title;
String author;
String publicationYear;
String status;
String borrower;
String borrowDate;
String returnDate;
Scanner userInput = new Scanner(System.in);
final String displayManual(){
String ManualInfo ="\nSerial Number: ........... "+serial+
"\nTitle: ................... "+title+
"\nAuthor: .................. "+author+
"\nPublication Year: ........ "+publicationYear+
"\nStatus: .................. "+status+
"\nBorrower: ................ "+borrower+
"\nDate Borrowed: ........... "+borrowDate+
"\nReturn date: ............. "+returnDate+
"\n";
return ManualInfo;
}
final void createManual(){
serial = Console.readString(Messages.enterSerialMessage, Messages.tooShortMessage, 3);
title = Console.readString(Messages.enterTitleMessage, Messages.tooShortMessage, 2);
author = Console.readString(Messages.enterAuthorMessage, Messages.tooShortMessage, 3);
publicationYear = Console.readString(Messages.enterPublicationYearMessage, Messages.tooShortMessage, 4);
borrower = "N/A";
borrowDate = "N/A";
returnDate = "N/A";
status = "Available";
}
...
if(Menu.menuChoice == 7){
boolean loadYesNo = Console.readYesNo("\n\nThe manualKeeper app is able to load and display any 'Library.txt' files \nfound in your home folder directory.\n\nWould you like to load and display library? (Y/N):\n");
String fileName = "Library.txt";
String line;
if(loadYesNo==true){
try {
BufferedReader input = new BufferedReader (new FileReader (fileName));
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) != null) {
Library.ManualList.add(line);
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
Menu.displayMenu();
} else if(loadYesNo==false){
System.out.println("\n\n--------------------------------------------------------------------------");
System.out.println("\n Library not loaded!\n");
System.out.println("--------------------------------------------------------------------------\n");
Menu.displayMenu();
}
}
...
if(Menu.menuChoice == 0){
if(Menu.menuChoice == 0){
if(Library.ManualList.size() > 0){
boolean saveYesNo = Console.readYesNo("\nThe manualKeeper app is able to save your current library to a '.txt' \nfile in your home folder directory (C:\\Users\\ 'YOUR NAME').\n\nWould you like to save the current library? (Y/N):\n");
File fileName = new File ("Library.txt");
if(saveYesNo==true){
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
for (int i = 0; i < Library.ManualList.size(); i++){
output.write(Library.ManualList.get(i).displayManual() + "\n");
}
output.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "I cannot create that file!");
}
}
else if(saveYesNo==false){
System.out.println("\n\n--------------------------------------------------------------------------");
System.out.println("\n Library not saved!\n");
System.out.println("--------------------------------------------------------------------------\n");
break exit;
}
Menu.displayMenu();
}else if(Library.ManualList.isEmpty()){
Menu.displayMenu();
}
}
}
}
System.out.println("\n ~ You have exited the manualKeeper app! ~ ");
System.out.println("\n Developed by Oscar Moore - 2014 - UWL\n");
System.out.println("\n <3\n");
}
}
Here is my library class if needed: 如果需要,这是我的图书馆课:
package library;
import java.util.ArrayList;
public class Library {
public static int ManualChoice;
static String returnManualTitle;
static String status1 = "Available";
static String status2 = "Borrowed";
public static ArrayList<Manual> ManualList = new ArrayList<Manual>();
static ArrayList<Manual> borrowedManuals = new ArrayList<Manual>();
static void addManual(){
Manual newManual = new Manual();
newManual.createManual();
ManualList.add(newManual);
System.out.println("\n\n--------------------------------------------------------------------------");
System.out.println("\n Manual added to library!\n");
System.out.println("--------------------------------------------------------------------------\n");
}
static void displayManualList(){
if (ManualList.isEmpty()){
System.out.println("-------------------------------------------------------------");
System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);
System.out.println("-------------------------------------------------------------");
Menu.menuChoice = 8;
} else {
System.out.printf("\n\nHere are the Manual/s currently stored in the library:\n\n\n");
for (int i = 0; i < ManualList.size(); i++){
System.out.printf("-------------------- Index Number: %s --------------------\n",i);
System.out.println(ManualList.get(i).displayManual());
System.out.println("---------------------------------------------------------\n");
}
}
}
static void displayBorrowedManuals(){
if (ManualList.isEmpty()){
System.out.println("-------------------------------------------------------------");
System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);
System.out.println("-------------------------------------------------------------");
Menu.menuChoice = 8;
} else {
for (int i = 0; i < borrowedManuals.size(); i++){
System.out.printf("-------------------- Index Number: %s --------------------\n",i);
System.out.println(borrowedManuals.get(i).displayManual());
System.out.println("---------------------------------------------------------");
}
}
}
public static void borrowManual(){
displayManualList();
ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size() - 1));
borrowLoop:
while(Menu.menuChoice == 3){
if ((ManualList.get(ManualChoice).status.equalsIgnoreCase(status1)) && (ManualList.size() >= ManualChoice)){
ManualList.get(ManualChoice).status = "Borrowed";
ManualList.get(ManualChoice).borrower = User.userName;
ManualList.get(ManualChoice).borrowDate = "Today.";
ManualList.get(ManualChoice).returnDate = "In two weeks.";
borrowedManuals.add(ManualList.get(ManualChoice));
System.out.println("\n--------------------------------------------------------------------------");
System.out.println("\n Manual borrowed!\n");
System.out.println("--------------------------------------------------------------------------\n");
break borrowLoop;
}else if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && ManualList.size() >= ManualChoice){
System.out.println("\n--------------------------------------------------------------------------");
System.out.println("\n "
+ " The Manual you wish to borrow is already on loan.");
System.out.println("\n--------------------------------------------------------------------------\n");
break borrowLoop;
}else if(ManualChoice > ManualList.size()-1){
System.out.println(Messages.noSuchManualMessage);
break borrowLoop;
}
if(ManualList.size() > 1){
displayManualList();
}
else if(ManualList.size() == 1){
ManualList.get(ManualChoice).status = "Borrowed";
ManualList.get(ManualChoice).borrower = User.userName;
ManualList.get(ManualChoice).borrowDate = "Today.";
ManualList.get(ManualChoice).returnDate = "In two weeks.";
borrowedManuals.add(ManualList.get(ManualChoice));
System.out.printf("\n\n %s\n\n", ManualList.get(ManualChoice).displayManual());
System.out.println("Please return the Manual within two weeks!\n");
displayManualList();
}
}
Menu.displayMenu();
}
static void returnManual(){
System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");
if(borrowedManuals.size() > 0){
for (int i = 0; i < borrowedManuals.size(); i++)
System.out.println(borrowedManuals.get(i).displayManual());
returnManualTitle = Console.readString(Messages.enterManualSerial, Messages.tooShortMessage, 3);
}
int x = 0;
boolean serialExistance = false;
while (x < ManualList.size()){
if (ManualList.get(x).serial.equalsIgnoreCase(returnManualTitle)){
ManualList.get(x).status = "Available";
ManualList.get(x).borrower = "N/A";
ManualList.get(x).borrowDate = "N/A";
ManualList.get(x).returnDate = "N/A";
int p = 0;
while (p < borrowedManuals.size()) {
Manual borrowed = borrowedManuals.get(p);
if (borrowed.serial.equalsIgnoreCase(returnManualTitle)) {
borrowedManuals.remove(p);
break;
}
p++;
}
System.out.println(Messages.successReturnMessage);
serialExistance = true;
break;
}
x = x+1;
}
if(serialExistance == false){
boolean repeatReturnManual = Console.readYesNo("\n--------------------------------------------------------------------------" + "\n\nThe Manual with the serial "+"\""+returnManualTitle +"\""+ " wasn't found!"
+"\n\nDo you want to try again? (Y/N):\n");
System.out.println("\n--------------------------------------------------------------------------");
if(repeatReturnManual){
returnManual();
}
}else if(serialExistance){
Menu.menuChoice = 8;
}
}
public static void removeManual(){
if(ManualList.size() >0){
displayManualList();
ManualChoice = Console.readInteger(Messages.enterRemoveManualIndex ,Messages.ManualIndexNotInListMessage, 0, ManualList.size());
int p = 0;
while (p < borrowedManuals.size()){
if (borrowedManuals.get(p).title.equalsIgnoreCase(returnManualTitle)){
borrowedManuals.remove(p);
}
}
ManualList.remove(ManualChoice);
System.out.print(Messages.successRemovedManualMessages);
Menu.menuChoice = 8;
}
}
static void emptyLibrary(){
System.out.println("\n WARNING!");
System.out.println("\n You have chosen to delete all Manuals in the library.\n");
System.out.println("--------------------------------------------------------------------------");
boolean emptyLibraryChoice = Console.readYesNo("\nAre you sure you wish to destroy the library? (Y/N): \n");
System.out.println("\n--------------------------------------------------------------------------\n");
if(emptyLibraryChoice){
Library.ManualList.clear();
System.out.println(Messages.successEmptyLibraryMesssage);
System.out.println("--------------------------------------------------------------------------\n");
Menu.menuChoice = 8;
}
}
}
The error is on the "Library.ManualList..." line. 错误在“ Library.ManualList ...”行上。 I am new to Java so can anyone help me get this working? 我是Java新手,所以有人可以帮助我完成这项工作吗? :( :(
I do know know what the method signature looks like for this code segment, or what the Library
class looks like, however I believe that the problem might lie in the declaration of the ManualList
in the Library
class. 我确实知道该代码段的方法签名是什么样的,或者Library
类是什么样的,但是我相信问题可能ManualList
在Library
类中ManualList
的声明中。 ManualList
should be both public
and static
, and that should give you the ability to access it in another class ManualList
应该既是public
又是static
,这应该使您能够在另一个类中访问它
Edit: I found in this line: Library.ManualList.add(line);
编辑:我在这一行中找到: Library.ManualList.add(line);
you are adding a string to an array that holds a custom type. 您正在向包含自定义类型的数组添加字符串。 Otherwise, compiling the code myself I found no problems. 否则,我自己编译代码就没有问题。 This of course is assuming that the classes are in the same package, and I had to comment out alot of the code that I didn't have ( such as all the Menu
references). 当然,这是假设这些类在同一包中,并且我不得不注释掉很多我没有的代码(例如所有Menu
引用)。 But it compiled for me. 但它为我编译。 I did not run it 我没有跑
Edit2: Something i just thought of, the final
modifier might cause some problems? Edit2:我刚刚想到的东西, final
修饰符可能会引起一些问题? But it worked for me when I tested it 但是当我测试它对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.