Monday, 15 February 2010

java - Encrypt/Decrypt file. ASCII +1 for encryption/decryption -



java - Encrypt/Decrypt file. ASCII +1 for encryption/decryption -

i'm trying read in file of text, , "encrypt"/convert each letter +1 ascii table (i want "decrypt" -1 that). "a" become "b", "b" "c" , forth. need convert alphabetic letters (ignore else, print them is). i'm having troubles part of code:

for(int = 0; <= words.size(); i++) { for(int j = 0; j <= words.get(i).length(); j++) { char ch = ' '; ch = words.get(i).charat(j); ch += 1; morewords.add(ch); } fileout.print(morewords.get(i) + " "); }

i've figured out how +1 char, i'm not sure how add together in array or print out correctly (since "morewords.add(ch)" going add together char, instead of converting chars adding string). "words.get(i).length()" takes entire length of array "words", when want length of string @ position "i" in array, throws error since length of array longer string word. i've been stuck on hours , cannot figure out. i'm thinking maybe shouldn't read them in strings , should have read them in chars , might have been simpler?

public static void main(string[] args) { scanner in = new scanner(system.in); arraylist<string> words = new arraylist<string>(); arraylist<character> morewords = new arraylist<character>(); string filename = ""; //replace test file f; scanner filein; system.out.println("please come in file name encryption: "); //filename = in.nextline(); filename = "test.txt"; seek { //build file , attach scanner f = new file (filename); filein = new scanner (f); system.out.println(f.exists()); //for errors int counting = 0; //reads in indvidual strings. for(counting =0; filein.hasnext(); counting++) { words.add(filein.next()); system.out.println(words); } printwriter fileout = new printwriter ("backwards.txt"); for(int = 0; <= words.size(); i++) { for(int j = 0; j <= words.get(i).length(); j++) { char ch = ' '; ch = words.get(i).charat(j); ch += 1; morewords.add(ch); } fileout.print(morewords.get(i) + " "); } fileout.close(); } catch(filenotfoundexception e) { system.out.println("couldn't find file"); } }

first in loops right

for (int = 0; <= words.size()-1; i++){}

if you'r starting @ 0 end @ length-1

what have changed is

printwriter fileout = new printwriter("c:/backwards.txt"); (int = 0; <= words.size()-1; i++) { (int j = 0; j <= words.get(i).length()-1; j++) { char ch = ' '; ch = words.get(i).charat(j); ch ++; // +=1 morewords.add(ch); fileout.print(ch); } fileout.print(" "); } fileout.close();

and output right if have understood right =)

this code

public static void main(string[] args) throws exception { bufferedreader inchannel = new bufferedreader(new filereader("c:/script.txt")); bufferedwriter outchannel = new bufferedwriter(new filewriter("c:/output.txt")); string toparse = ""; while ( (toparse = inchannel.readline()) != null ) { string towrite = ""; for(int i=0; i!=toparse.length();i++) { char c = toparse.charat(i); if(true) //check if must encoded or not { c++; towrite += c; } } outchannel.write(towrite); outchannel.newline(); } inchannel.close(); outchannel.close(); }

hope helped

java encryption file-io ascii

No comments:

Post a Comment