What I need to do is use the args command to choose which method to go to. For instance, I have methods encrypt, decrypt, and crack. The user will open their command and prompt this:
Where myCodeName is the name of my program, methodchoice is either encrypt, decrypt or crack; inputfile is whatever file they want to input, outputfile whatever they want to output and key is the key to the encryption (not necessary for using the crack method)C:\Users\Name\File>java myCodeName methodchoice inputfile outputfile key
I'm just unsure how to pull each string from the args in order so that I can do repeated if loops or something like that. Here is my code:
public class Encrypt { public static void main(String[] args) { String command = args; if (command.equalsIgnoreCase(encrypt) { } } public static void encrypt(char[] array, int key) { try { FileWriter filewriter = new FileWriter(new File("")); for (int i = 0; i < array.length; i++) { char space = ' '; if (array[i] != space) { int charAsNum = (int) array[i] -97; int decode = charAsNum + key; if (decode > 25) { decode = decode % key; } decode = decode + 97; char numAsChar = (char)decode; filewriter.write(numAsChar); } else { char numAsChar = ' '; filewriter.write(numAsChar); } } filewriter.close(); } catch (java.io.IOException ex) { System.out.println("File not created"); System.exit(-1); } } public static void decrypt(char[] array, int key) { try { FileWriter filewriter = new FileWriter(new File("decrypt.txt")); for (int i = 0; i < array.length; i++) { char space = ' '; if (array[i] != space) { int charAsNum = (int) array[i] -97; int decode = charAsNum - key; if (decode < 0) { decode = decode + 25; } if (decode > 25) { decode = decode % key; } decode = decode + 97; char numAsChar = (char)decode; filewriter.write(numAsChar); } else { char numAsChar = ' '; filewriter.write(numAsChar); } } filewriter.close(); } catch (java.io.IOException ex) { System.out.println("File not created"); System.exit(-1); } } public static void crack(char[] array) { int[] code = new int[array.length]; for (int i = 0; i < array.length; i++) { char space = ' '; if (array[i] != space) { code[i] = (int)array[i] - 97; } if (array[i] == space) { code[i] = 27; } } int[] count = new int[28]; for (int i = 0; i <code.length; i++) { count[code[i]]++; } int max = count[0]; int indexOfMax = 0; for (int i = 1; i < count.length; i++) { if (count[i] > max && count[i] < count[27]) { max = count[i]; indexOfMax = i; } } indexOfMax = indexOfMax + 97; int key = 101 - indexOfMax; key = key * -1; decrypt(array ,key); } } }
I am also unsure on how to turn the filewriter into the name they want, but I'm fairly certain that can be done by adding a file x to the method's parameters itself.