Java: Switch statements with methods involving arrays

Posted by Shane on Stack Overflow See other posts from Stack Overflow or by Shane
Published on 2012-06-10T16:24:50Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 218

Filed under:
|
|

Hi guys I'm currently creating a program that allows the user to create an array, search an array and delete an element from an array. Looking at the LibraryMenu Method, the first case where you create an array in the switch statement works fine, however the other ones create a "cannot find symbol error" when I try to compile.

My question is I want the search and delete functions to refer to the first switch case - the create Library array. Any help is appreciated, even if its likely from a simple mistake.

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

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays