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.