大家好我正在创建一个允许用户创建数组,搜索数组并从数组中删除元素的程序。查看LibraryMenu
方法,在switch语句中创建数组的第一种情况正常,但是当我尝试编译时,其他情况会创建“找不到符号错误”。
我的问题是我希望搜索和删除功能引用第一个开关案例 - 创建库数组。任何帮助都会受到赞赏,即使它可能来自一个简单的错误。
import java.util.*;
public class EnterLibrary
{
public static void LibraryMenu()
{
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
LibraryMenu Menu = new LibraryMenu();
Menu.displayMenu();
switch (scannerObject.nextInt() )
{
case '1':
{
System.out.println ("1 - Add Videos");
Library[] newLibrary;
newLibrary = createLibrary();
}
break;
case '2':
System.out.println ("2 - Search Videos");
searchLibrary(newLibrary);
break;
case '3':
{
System.out.println ("3 - Change Videos");
//Change video method TBA
}
break;
case '4':
System.out.println ("4 - Delete Videos");
deleteVideo(newLibrary);
break;
default:
System.out.println ("Unrecognized option - please select options 1-3 ");
break;
}
}
public static Library[] createLibrary()
{
Library[] videos = new Library[4];
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int i = 0; i < videos.length; i++)
{
//User enters values into set methods in Library class
System.out.print("Enter video number: " + (i+1) + "\n");
String number = scannerObject.nextLine();
System.out.print("Enter video title: " + (i+1) + "\n");
String title = scannerObject.nextLine();
System.out.print("Enter video publisher: " + (i+1) + "\n");
String publisher = scannerObject.nextLine();
System.out.print("Enter video duration: " + (i+1) + "\n");
String duration = scannerObject.nextLine();
System.out.print("Enter video date: " + (i+1) + "\n");
String date= scannerObject.nextLine();
System.out.print("VIDEO " + (i+1) + " ENTRY ADDED " + "\n \n");
//Initialize arrays
videos[i] = new Library ();
videos[i].setVideo( number, title, publisher, duration, date );
}
return videos;
}
public static void printVidLibrary( Library[] videos)
{
//Get methods to print results
System.out.print("\n======VIDEO CATALOGUE====== \n");
for (int i = 0; i < videos.length; i++)
{
System.out.print("Video number " + (i+1) + ": \n" + videos[i].getNumber() + "\n ");
System.out.print("Video title " + (i+1) + ": \n" + videos[i].getTitle() + "\n ");
System.out.print("Video publisher " + (i+1) + ": \n" + videos[i].getPublisher() + "\n ");
System.out.print("Video duration " + (i+1) + ": \n" + videos[i].getDuration() + "\n ");
System.out.print("Video date " + (i+1) + ": \n" + videos[i].getDate() + "\n ");
}
}
public static Library searchLibrary( Library[] videos)
{
//User enters values to setSearch
Library titleResult = new Library();
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int n = 0; n < videos.length; n++)
{
System.out.println("Search for video number:\n");
String newSearch = scannerObject.nextLine();
titleResult.getSearch( videos, newSearch);
if (!titleResult.equals(-1))
{
System.out.print("Match found!\n" + newSearch + "\n");
}
else if (titleResult.equals(-1))
{
System.out.print("Sorry, no matches found!\n");
}
}
return titleResult;
}
public static void deleteVideo( Library[] videos)
{
Library titleResult = new Library();
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int n = 0; n < videos.length; n++)
{
System.out.println("Search for video number:\n");
String deleteSearch = scannerObject.nextLine();
titleResult.deleteVideo(videos, deleteSearch);
System.out.print("Video deleted\n");
}
}
public static void main(String[] args)
{
Library[] newLibrary;
new LibraryMenu();
}
}
答案 0 :(得分:2)
我认为这是一个糟糕的设计。你把很多东西混合在一起:用户界面,逻辑,数据结构。
首先将LibraryArray
与LibraryMenu
隔离开来。您根本不应该看到任何开关或输入或输出。
Java是一种面向对象的语言。从对象开始考虑您的系统。我没有看到Video
和VideoCatalog
等类。如果你创建它们,你会发现这个系统更容易实现。
看起来你有一个开始:
package model;
public class Video {
private Long id;
private String title;
private String publisher;
private int durationSeconds;
private Date publicationDate;
// add ctors, getters, etc. Immutable? Could be...
// equals, hash code, toString
}
保持您的VideoCatalog不受用户界面或I / O的影响:
package model;
public interface VideoCatalog {
List<Video> find();
List<Video> find(String title);
List<Video> find(Date startDate, Date endDate) ;
Long save(Video video);
void update(Video video);
void delete(Video video);
}
现在您可以拥有一个使用您想要的任何数据结构的实现:
package model;
public class VideoCatalogImpl implements VideoCatalog {
private Set<Video> videos;
// add implementations here.
}
答案 1 :(得分:1)
您需要将该数组变量的声明移出第一个case
的范围,直到其他情况可以看到的位置。鉴于代码的当前结构,最方便的是使它成为类的静态成员 - 即。,
public class EnterLibrary
{
Library[] newLibrary;
然后这个类的所有静态方法都可以共享一个变量。但是一定要删除其他方法中出现的变量的所有其他声明,否则它们仍然会使用单独的变量,这样的bug很难追踪!
答案 2 :(得分:0)
试试这个,
声明Library[] newLibrary;
as an instance variable (at class scope)
或as local variable before the switch statement
。
答案 3 :(得分:0)
Library[] newLibrary;
仅在您的案例“1”中定义,您应该在更广泛的范围内定义它,例如LibraryMenu
方法。此外,在您的main中声明的Library[] newLibrary
不会在任何地方调用,也许您应该在搜索中添加Null检查,打印删除方法。
您的构造函数类必须与您的类具有相同的名称,并且其中没有任何修饰符关键字。此外,当您创建类的对象时,它不会使用那里声明的静态方法。
注意:当您使用自己声明的数组时,最好声明一个int
变量来跟踪数组的实际大小。请注意,array.length
返回数组可以拥有的项目数,而不是已有的项目数。
我会将你的定义(而不是代码)重新设计成这样的东西:
//Note I changed the classname from EnterLibrary to LibraryMenu. Apparently you
//wanted a LibraryMenu class.
public class LibraryMenu {
private final int MAX_ITEMS = 50;
private Library[] videos;
private int size = 0;
//remove the static and void keyworkds from this method, so this will be
//the constructor.
public LibraryMenu() {
videos = new Library[MAX_ITEMS];
//the rest of your code here...
switch (scannerObject.nextInt()) {
//if you're reading an int, keep the constants in the case as int.
case 1:
//no need of brackets inside a case statement
//check that you can add an item in your Library array
//also, its not good to ask the user to add 4 (or N) videos in 1 round :).
if (size < MAX_ITEMS) {
Library video = addVideo();
videos[size++] = video;
}
break;
case 2:
break;
}
}
//remove the static keyword so the instance of your class can call the method.
public Library addVideo() {
Library video = new Library();
//your code to read data goes here...
//then fulfill the video and return it.
return video;
}
//The Library[] videos is declared in your class, so all other methods could
//use it without having to receive it as a parameter.
public void printVidLibrary() {
//your code goes here...
}
public Library searchLibrary() {
//your code goes here...
}
public void deleteVideo( Library[] videos) {
//your code goes here...
}
public static void main(String[] args) {
new LibraryMenu();
}
}