Scanner restarting in Java -


my task read text file in chunks of 64 characters, , use 2 different processes called substitution , column transposition encrypt it. then, have decrypt , write out file.

i have written , tested out both processes of encrypting , decrypting , worked wonderfully. tried loop processes in case more 64 characters in input file.

as test case, tried 128 character input file. unfortunately, result gives me first 64 characters twice. i've tracked scanner position , goes beyond 64, characters read start 0. i'm not sure problem is.

here relevant part of code:

public static void main(string[] args) {	  		  		//declare variables  		scanner console = new scanner(system.in);  		string inputfilename = null;  		file inputfile = null;  		scanner in = null;  		  		do  		{  			//check if there enough arguments  			try  			{  				inputfilename = args[1];  			}  			catch (indexoutofboundsexception exception)  			{  				system.out.println("not enough arguments.");  				system.exit(1);  			}  			catch (exception exception)  			{  				system.out.println("there error. please try again.");  				system.exit(1);  			}  			  			//check if input file valid  			try  			{  				inputfile = new file(inputfilename);  				in = new scanner(inputfile);  				outputfile = new file(outputfilename);  				out = new scanner(outputfile);  			}  			catch (filenotfoundexception exception)  			{  				system.out.println("could not find input file.");  				system.exit(1);  			}  			catch (exception exception)  			{  				system.out.println("there error. please try again.");  				system.exit(1);  			}  		} while (outputfilename != null && !inputfile.exists());          //encryption      //prepare patterns  		string subpattern = createsubstitutionpattern(hash);  		int[] transpattern = createtranspositionpattern(hash);  		  		//apply patterns  		string textcontent = "";  		string applysub = "";  		string applytrans = "";  		do  		{  			textcontent = read64chars(in);  			applysub = applysub + applysubstitutionpattern(textcontent, subpattern);  			applytrans = applytrans + applytranspositionpattern(applysub, transpattern);  		} while (in.hasnext());        //decryption      string encryptcontent = "";  		scanner encrypt = new scanner(applytrans);  		string removetrans = "";  		string removesub = "";  		do  		{  			encryptcontent = read64chars(encrypt);  			system.out.println(applytrans);  			removetrans = removetrans + removetranspositionpattern(encryptcontent, transpattern);  			removesub = removesub + removesubstitutionpattern(removetrans, subpattern);  		} while (encrypt.hasnext());  		  		console.close();  		in.close();  		encrypt.close();    system.out.println(removesub); //for temporary testing    }    public static string read64chars (scanner in)  	{  		string textcontent = "";  		in.usedelimiter("");  		for (int x=0; x<64; x++)  		{  			if (in.hasnext())  			{  				textcontent = textcontent + in.next().charat(0);  			}  		}  		return textcontent;  	}

do note have more variables fill in args[0] , args[2] removed them simplicity.

i know if true once scanner reads portion of it's input, "consumes" it, , portion gets removed. scanner reset when declared again through method? example, declaration point input source of original scanner, or actual scanner current properties?

encrypt diffrent scanner in, advance 64 characters when first call read64chars. so, encrypt starts @ first character when call read64chars(encrypt). seems want use same scanner both times.

also, in future please name functions starting lowercase letter. felt dirty typing that... :)


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -