Java Caesar Cipher replacing a char in a string with an array value -
okay, school project attempting make method take file, use caesar cipher 'encrypt' it, , output new encrypted word output file, reading in words of file leave separate encrypted file.
the problem i'm running
"unexpected type. required: variable. found: value" error
whenever try replace character new one.
here's encryption method, enough find problem.
public static void encrypt(file inputfile, int key) throws filenotfoundexception { char[] alpha = new char[26]; for(char ch = 'a'; ch <= 'z'; ch++) { int = 0; alpha[i] = ch; i++; } scanner in = new scanner(inputfile); while(in.hasnext()) { string word = in.next(); word = word.tolowercase(); for(int = 0; < word.length(); i++) { for(int j = 0; j < alpha.length; j++) { if(word.charat(i) == alpha[j]) { word.charat(i) = alpha[j + key]; } } } } }
as strings immutable can not
word.charat(i) = alpha[j + key]; consider using stringbuilder instead, like
stringbuilder buf = new stringbuilder (); for(int = 0; < word.length(); i++) { for(int j = 0; j < alpha.length; j++) { if(word.charat(i) == alpha[j]) { buf.append(alpha[j + key]); } else { buf.append (word[i]); // ?? } } } // buf ??
Comments
Post a Comment