So the goal of the assignment (or this direct part I'm working on at the moment) is to take an input (in this case, the file just has 'a b c d' in it.) and using a key (say by adding 5) take the ASCII value and encrypt the document. So for example, after turning the file to a string - if the file said "abc" I want the encryption to change the array to first become the ASCII int equivalent, then add the key to that ASCII int (for example: 1) and then back to char to display, in this case, 'bcd'
In our lab, we did this example and it works the way it should:
import java.io.*; import java.util.*; public class Lab8b { public static void main(String[] args) { int charAsNum = 'z'; charAsNum = charAsNum - 97; int decode = charAsNum + 2; if (decode > 26) { decode = decode % 2; } decode = decode + 97; char numAsChar = (char)decode; System.out.println(numAsChar); } }
And it works perfectly. Using the above logic, this is my current code:
import java.util.*; import java.io.*; public class tester{ public static void main(String[] args) { char[] test = {'a','b','c','d'}; encrypt(test, 5); } public static void encrypt(char[] array, int key) { for (int i = 0; i < array.length; i++) { char space = ' '; if (array[i] == space) { // will keep array[i] as a space } else { int charAsNum = array[i]; charAsNum = charAsNum - 97; System.out.println(array[i]); } } for (int i = 0; i < array.length; i++) { int decode = array[i] + key; if (decode > 25) { decode = decode % key; } decode = decode + 97; char numAsChar = (char)decode; } } }
Nothing happens. What is it that I'm missing?