2012-06-10 61 views
0

嗨,大家好我目前正在創建一個程序,允許用戶創建一個數組,搜索一個數組並從數組中刪除一個元素。查看LibraryMenu方法,您在switch語句中創建數組的第一種情況可以正常工作,但是其他人在嘗試編譯時會創建一個「無法找到符號錯誤」。Java:用涉及數組的方法切換語句

我的問題是我想搜索和刪除函數來引用第一個開關案例 - 創建庫數組。任何幫助表示讚賞,即使它可能來自一個簡單的錯誤。

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(); 
} 
} 
+2

請把一些精力投入到正確的縮進你的代碼,我保證我會放一些精力投入到正確讀取你的整個問題。 :) –

回答

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(); 
    } 
} 
+0

這個答案似乎對我最有幫助。如果我再遇到問題,我會進一步評論。謝謝。 – Shane

+0

@Shane你可以[標記一個帖子作爲答案](http://meta.stackexchange.com/a/5235/182862)。我會盡我所能幫助你,如果你有新的社區問題,你應該做一個新的問題,而不是在這裏添加額外的問題。 –

1

你需要移動數組變量的聲明出來的第一case的範圍,以及高達某處,在其他情況下,可以看到它。鑑於你的代碼目前的結構,這將是最方便的,使之類的靜態成員 - 即

public class EnterLibrary 
{ 
    Library[] newLibrary; 

然後,所有這個類的靜態方法可以共享一個變量。但一定要刪除出現在其他方法中的變量的所有其他聲明,否則它們仍將使用單獨的變量,並且像這樣的錯誤可能很難追蹤到!

2

我認爲這是一個可怕的設計。你將許多東西混合在一起:用戶界面,邏輯,數據結構。

LibraryMenu開始隔離您的LibraryArray。你根本不應該看到任何開關或輸入或輸出。

Java的一種面向對象的語言。從對象的角度開始思考你的系統。我沒有看到類似VideoVideoCatalog。如果你創建了這個系統,你會發現這個系統更容易實現。

看起來你已經有了一個開始:

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. 
} 
0

試試這個,

聲明Library[] newLibrary;as an instance variable (at class scope)as local variable before the switch statement