I'm having an issue with my Switch in my while loop. My code allows a user to input a file path. That file path is stored as a static variable, and that variable will be used by the other methods to build the full file path with the name of specific file. Each method in the code works independently, and works outside of the while loop. However, when a couple of methods are selected, they will just produce an error. Here is my main method.
public static void main(String[] args) throws FileNotFoundException{
int option = 10;
menu();
while (option != 0){
System.out.println("Please Make a Selection.");
option = scan.nextInt();
switch (option){
case 0:
System.out.println("Thank you for using the program");
System.exit(0);
break;
case 1:
// user inputs a directory
selectDirectory();
// user directory will be stored as a static variable directory
break;
case 2:
contentFirstLevel(directory);
break;
case 3:
contentAllLevel(directory);
break;
case 4:
System.out.println("Input File to Delete");
String deletedFile = scan.nextLine();
deleteFile(deletedFile);
break;
case 5:
try {
System.out.println("Input file to Display.");
fileName = scan.nextLine();
fileToHex();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 6:
System.out.println("Input File to Encrypt");
String encryptedFile = scan.nextLine();
System.out.println("Input Password for the File");
String passWord = scan.nextLine();
try {
encryptFile(encryptedFile, passWord);
}catch (IOException e){
e.printStackTrace();
}catch (GeneralSecurityException e){
e.printStackTrace();
}
break;
case 7:
System.out.println("Input File to Decrypt");
String decryptedFile = scan.nextLine();
System.out.println("Input Password for the File");
String pass = scan.nextLine();
try {
decryptFile(decryptedFile, pass);
}catch (IOException e){
e.printStackTrace();
}catch (GeneralSecurityException e){
e.printStackTrace();
}
}
}
scan.close();
}
The issue I'm having is with Case 5. When it is selected, it will just go straight into errors and doesn't let the user input the specific file name for the file to be displayed. Here is the method used in case 5.
public static void fileToHex() throws Exception{
try (FileInputStream fis = new FileInputStream(fullFile)) {
int i = 0;
int count = 0;
while ((i = fis.read()) != -1) {
System.out.printf("%02X ", i);
count++;
if (count == 16) {
System.out.println();
count = 0;
}
}
}
}