java - Trying to decipher a text file with an algorithm -
i have deciphering method should open test file encrypted text, read , decipher each line of text read in input file. text file called mystery.txt. can method work when inputting single characters can't work open .txt file , decipher line line. have tried calling readfile() before algorithm null pointer exception in (i = 0; < text.length(); i++) . have never done similar before , don't know how go doing it.
the algorithm should read each line , decipher using crypt1 & crypt2 variables.
using readfile() , writefile() methods, need cipherdecipherstring() method able write method opens file, reads each line contains 1 @ time, deciphers each line read in , writes resulting text screen , output file.
dechiphering method:
public static string cipherdecipherstring(string text) { // these global. put here space saving private static final string crypt1 = "cipherabdfgjk"; private static final string crypt2 = "lmnoqstuvwxyz"; // declare variables int i, j; boolean found = false; string temp="" ; // empty string hold converted text readfile(); (i = 0; < text.length(); i++) // @ every chracter in text { found = false; if ((j = crypt1.indexof(text.charat(i))) > -1) // char in crypt1? { found = true; // yes! temp = temp + crypt2.charat(j); // add cipher character temp } else if ((j = crypt2.indexof(text.charat(i))) > -1) // , on { found = true; temp = temp + crypt1.charat(j); } if (! found) // deal cases char not in crypt2 or 2 { temp = temp + text.charat(i); // copy across character } } return temp; }
my readfile method:
public static void readfile() { filereader filereader = null; bufferedreader bufferedreader = null; string inputfilename; string nextline; clrscr(); system.out.println("please enter name of file read (e.g. afile.txt: "); inputfilename = genio.getstring(); try { filereader = new filereader(inputfilename); bufferedreader = new bufferedreader(filereader); nextline = bufferedreader.readline(); while (nextline != null) { system.out.println(nextline); nextline = bufferedreader.readline(); } } catch (ioexception e) { system.out.println("sorry, there has been problem opening or reading file"); } { if (bufferedreader != null) { try { bufferedreader.close(); } catch (ioexception e) { system.out.println("an error occurred when attempting close file"); } } } }
my writefile() method:
public void writefile() { string mystring; clrscr(); system.out.println("begin typing contents wish write file: "); mystring = genio.getstring(); system.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); fileoutputstream outputstream = null; printwriter printwriter = null; try { outputstream = new fileoutputstream("writing.txt"); printwriter = new printwriter(outputstream); printwriter.write(mystring); printwriter.println("\n"); while (!mystring.equals("")) { mystring = genio.getstring(); printwriter.print(mystring + "\n"); } system.out.println("file: 'writing.txt' has been saved contents above.\n\nyou can open file using other options in menu screen."); presskey(); } catch (ioexception e) { system.out.println("sorry, there has been problem opening or writing file"); } { if (printwriter != null) { // close file printwriter.close(); } } }
now thought able call readfile() method go decipher code , let work it's way through file cannot work @ all.
genio class deals user input. didn't want go overkill code can post add if it's needed. thank you!
lets call cipherdecipherstring
decode
, put in method called decode
takes 2 file paths. 1 source file path, other destination. here's how code like:
private static final string crypt1 = "cipherabdfgjk"; private static final string crypt2 = "lmnoqstuvwxyz"; private string decode (string line) { stringbuilder result = new stringbuilder (line.length ()); (int = 0; < line.length (); ++i) { char ch = line.charat (i); int index1 = crypt1.indexof (ch); int index2 = crypt2.indexof (ch); if (index1 != -1) { result.append (crypt2.charat (index1)); } else if (index2 != -1) { result.append (crypt1.charat (index2)); } else { result.append (ch); } } return result.tostring (); } /* takes file source, reads line line, decodes each line, writes * decoded lines file destination. */ public void decode (string filesource, string filedestination) throws ioexception { files.write (paths.get (filedestination), files.lines (paths.get (filesource)). map (this::decode). collect (collectors.tolist ())); }
this requires java 8 though. if have issues in cipherdecipherstring
method, let me know.
these required imports:
import java.io.ioexception; import java.nio.file.files; import java.nio.file.paths; import java.util.stream.collectors;
Comments
Post a Comment