1

I am trying to write a menu to handle my classes for an assignment. I have the classes I want to try but I seem to have a problem creating the menu.

 switch(choice)
 {
    case 1:
        System.out.print("Please enter a filename: ");
        filename = option.next();

         //creating objects of the file manager to open the required files
        FileManager e = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\"+filename+".txt");
        FileManager e1 = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\punctuation.txt");

        //creating connection to the files
        e.connectToFile();
        e1.connectToFile();

        //Reading the files
        String[] fileToBeRead= e.readFile();
        String[] punctMarks = e1.readFile();

        //closing the files
        e.closeReadFile();
        e1.closeReadFile();
        fileRead++;
        break;

    case 2:

        if(fileRead == 0)
        {
            System.out.println("No file was read. Please open a file to correct this error:");

        }
        else
        {
            FindLan t3 = new FindLan(fileToBeRead);
            t3.cLang();
        }
        break;

    case 3:
        if(fileRead == 0)
        {
            System.out.println("No file was read. Please open a file to correct this error:");

        }
        else
        {

            //Creating the object for the remove punctuation class
            RemovePunct t1 = new RemovePunct(punctMarks, fileToBeRead);

            //Calling the EndArray(Remove) method to clean an array of punctuation marks
            String[] cleanWords = t1.EndArray();

             for(int i=0; i<10; i++)
             {
                System.out.println(cleanWords[i]);
             }
        }
    default:
        System.out.println("Option is not available");
 }       

So I need to be able to use the variables from case 1 in case 2 and 3 but I need them initialized in case 1 to get the length of the array.

So far I tried to use a try/catch block but that doesn't seem to solve the problem. Any other ideas on how I could used the initialised values from case one in the other cases without having to give them a value in case 2/3?

The main goal is to be able to use the arrays defined in case 1 that get their size and elements from the filemanager class and use them in the other 2 cases without me having to define size or elements.

1
  • The overall goal is to have the arrays receive their size from my file manager class. I am setting the size and elements in there and returning it therefore i can't initialize it outside the switch. furthermore i need to use those returned arrays in the case2, case 3 for my other classes. So i want to be able to use the two arrays from case 1 in case2 and case3 without having to give them a value myself Commented Mar 16, 2017 at 18:57

1 Answer 1

2

Move the variable declarations outside the switch statement like this:

FileManager e = null;
FileManager e1 = null;
String[] fileToBeRead;
String[] punctMarks;

switch(choice)
{
    case 1:
        System.out.print("Please enter a filename: ");
        filename = option.next();

         //creating objects of the file manager to open the required files
        e = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\"+filename+".txt");
        e1 = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\punctuation.txt");

        //creating connection to the files
        e.connectToFile();
        e1.connectToFile();

        //Reading the files
        fileToBeRead= e.readFile();
        punctMarks = e1.readFile();

        //closing the files
        e.closeReadFile();
        e1.closeReadFile();
        fileRead++;
        break;

    case 2:

        if(fileRead == 0)
        {
            System.out.println("No file was read. Please open a file to correct this error:");

        }
        else
        {
            FindLan t3 = new FindLan(fileToBeRead);
            t3.cLang();
        }
        break;

    case 3:
        if(fileRead == 0)
        {
            System.out.println("No file was read. Please open a file to correct this error:");

        }
        else
        {

            //Creating the object for the remove punctuation class
            RemovePunct t1 = new RemovePunct(punctMarks, fileToBeRead);

            //Calling the EndArray(Remove) method to clean an array of punctuation marks
            String[] cleanWords = t1.EndArray();

             for(int i=0; i<10; i++)
             {
                System.out.println(cleanWords[i]);
             }
        }
    default:
        System.out.println("Option is not available");
 }
Sign up to request clarification or add additional context in comments.

2 Comments

It is not enough. It will not pass compiler since again variables can be not initialized. One of the way is to give them null values at the declaration. like FileManager e = null; It will pass compiler, but it needs to handle situation in case 2 and 3 when variable is null.
It's fine thank you I just needed to bypass the compiler error I will use ifs to make sure that the variables are initialised. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.